栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > C/C++/C#

Java 面向对象 习题1(基础篇)

C/C++/C# 更新时间:发布时间: 百科书网 趣学号

活动地址:CSDN21天学习挑战赛

题目目录
  • 1、编写Address类
  • 2、定义Employee类
  • 3、编写程序,统计字符串
  • 4、设计Dog类
  • 5、设计User类

1、编写Address类

题目:编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息。

代码如下:

class Address{
    private String country,province,city,street,postcode;
    public Address(String country,String province,String city,String street,String postcode){
        this.setCountry(country);
        this.setProvince(province);
        this.setCity(city);
        this.setStreet(street);
        this.setPostcode(postcode);
    }
    public void tell(){
        System.out.println("Address:"+getCountry()+","+getProvince()+","+getCity()+","+getStreet()+","+getPostcode());
    }
    public void setCountry (String country) {
        this.country = country;
    }
    public void setProvince (String province) {
        this.province = province;
    }
    public void setCity (String city) {
        this.city = city;
    }
    public void setStreet (String street) {
        this.street = street;
    }
    public void setPostcode (String postcode) {
        this.postcode = postcode;
    }
    public String getCountry () {
        return country;
    }
    public String getProvince () {
        return province;
    }
    public String getCity () {
        return city;
    }
    public String getStreet () {
        return street;
    }
    public String getPostcode () {
        return postcode;
    }
}
public class Demo1 {
    public static void main(String[] args){
        Address add=new Address("中国","甘肃","兰州","中北路","12345");
        add.tell();
    }
}

运行结果:

Address:中国,甘肃,兰州,中北路,12345

2、定义Employee类

题目:定义并测试一个代表员工的Employee类。员工属性包括“编号"、“姓名" 、“基本薪水”、“薪水增长额",还包括计算薪水增长额及计算增长后的工资总额的操作方法。

代码如下:

class Employee{
    private String id,name;
    private double salary,growth;
    public Employee(String id,String name,double salary){
        this.setId(id);
        this.setName(name);
        this.setSalary(salary);
    }
    public void setId(String id){
        this.id=id;
    }
    public void setName(String name){
        this.name=name;
    }
    public void setSalary(double salary){
        this.salary=salary;
    }
    public String getId(){
        return id;
    }
    public String getName(){
        return name;
    }
    public double getSalary(){
        return salary;
    }
    public double growth(){
        growth=salary*2+500;
        return growth;
    }
    public double total(){
        return salary+growth;
    }
    public void print(){
        System.out.println("编号:"+getId()+", 姓名:"+getName()+", 基本薪水:"+getSalary()+", 薪水增长额:"+growth()+", 工资:"+total());
    }
}
public class Demo2 {
    public static void main(String[] args){
        Employee emp=new Employee("123","张三",3000);
        emp.print();
    }
}

运行结果:

编号:123, 姓名:张三, 基本薪水:3000.0, 薪水增长额:6500.0, 工资:9500.0

3、编写程序,统计字符串

题目:编写程序,统计出字符串"want you to know one thing"中字母n和字母0的出现次数。

代码如下:

public class Demo3 {
    public static void main(String[] args){
        String str="want you to know one thing";
        char[] c=str.toCharArray();
        int countn,counto;
        countn=counto=0;
        for(int i=0;i
            if(c[i]=='n'){
                countn++;
            } else if(c[i]=='o') {
                counto++;
            }
        }
        System.out.println("字母n出现的次数:"+countn);
        System.out.println("字母o出现的次数:"+counto);
    }
}

运行结果:

字母n出现的次数:4
字母o出现的次数:4

4、设计Dog类

题目:设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类。

代码如下:

class Dog{
    private String name,color;
    private int age;
    public Dog(String name,String color,int age){
        this.name=name;
        this.color=color;
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public String getColor(){
        return this.color;
    }
    public int getAge(){
        return this.age;
    }
    public void print(){
        System.out.println("名字:"+getName()+", 颜色:"+getColor()+", 年龄:"+getAge());
    }
}
public class Demo4 {
    public static void main(String[] args){
        Dog dog1=new Dog("巴克","黄色",5);
        Dog dog2=new Dog("克鲁","黑色",6);
        dog1.print();
        dog2.print();
    }
}

运行结果:

名字:巴克, 颜色:黄色, 年龄:5
名字:克鲁, 颜色:黑色, 年龄:6

5、设计User类

题目:设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的3个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置囗令的方法和返回类信息的方法。

代码如下:

//设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,
//定义类的3个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置囗令的方法和返回类信息的方法。
class User{
    private String name,command;
    private static int number;//定义静态变量number,共享数据
    public User(){
        number++;
    }
    public User(String name){
        this.name=name;
        number++;
    }
    public User(String name,String command){
        this.setName(name);
        this.setCommand(command);
        number++;
    }
    public void setName(String n){
        name=n;
    }
    public String getName(){
        return name;
    }
    public void setCommand(String c){
        command=c;
    }
    public String getCommand(){
        return command;
    }
    public void getUser(){
        System.out.println("用户名:"+getName()+",口令:"+getCommand());
    }
    public static int getNumber(){    //静态方法,方便调用
        return number;
    }
}
public class Demo5 {
    public static void main(String[] args){
        User user1=new User();
        User user2=new User("张三");
        User user3=new User("李四","你好!");
        user1.getUser();
        user2.getUser();
        user3.getUser();
        System.out.println("用户个数:"+User.getNumber());//静态方法,通过类名调用
    }
}

运行结果:

用户名:null,口令:null
用户名:张三,口令:null
用户名:李四,口令:你好!
用户个数:3

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1033972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号