Java基础(四)(根据尚硅谷宋红康老师Java基础第四部分)(重点)

Java面向对象编程(上)

主要讲解宋老师里面课后题(无答案的),以便后期复习使用。(可以私信我要文档资料)

代码题(差不多的就一次)

  1. 编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
    类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
    在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出。
public class Student {
    private String name;
    private String gender;
    private int age;
    private int id;
    private double score;

    public Student(String name, String gender, int age, int id, double score) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.id = id;
        this.score = score;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public String say() {
        return "{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", id=" + id +
                ", score=" + score +
                '}';
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }


}
public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student("张三","男",18,1,98.0);
        System.out.println(student.say());
    }
}
  1. 定义一个丈夫Husband类,有姓名、年龄、妻子属性
    定义一个妻子Wife类,有姓名、年龄、丈夫属性
    丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
    妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
    定义一个测试类,创建妻子和丈夫对象,然后测试
public class Husband {
    private String name;
    private int age;
    private Wife wife;


    public String getInfo() {
        return "{" +
                "husband的name='" + name + '\'' +
                ",husband的age=" + age +
                ", wife的name=" + wife.getName() +",wife的age="+wife.getAge()+
                '}';
    }

    public Husband() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Wife getWife() {
        return wife;
    }

    public void setWife(Wife wife) {
        this.wife = wife;
    }

    public Husband(String name, int age, Wife wife) {
        this.name = name;
        this.age = age;
        this.wife = wife;
    }
}
public class Wife {
    private String name;
    private int age;
    private Husband husband;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Husband getHusband() {
        return husband;
    }

    public void setHusband(Husband husband) {
        this.husband = husband;
    }


    public String getInfo() {
        return "{" +
                "wife的name='" + name + '\'' +
                ",wife的age=" + age +
                ", husband的name=" + husband.getName() +",husband的name="+husband.getAge()+
                '}';
    }

    public Wife() {
    }

    public Wife(String name, int age, Husband husband) {
        this.name = name;
        this.age = age;
        this.husband = husband;
    }
}
public class Exer2Test {
    public static void main(String[] args) {

        Husband husband = new Husband();
        husband.setName("张三");
        husband.setAge(22);
        Wife wife  = new Wife();
        wife.setName("李四");
        wife.setAge(18);
        husband.setWife(wife);
        wife.setHusband(husband);


        System.out.println(husband.getInfo());
        System.out.println(wife.getInfo());
    }
}
  1. 定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer
    银行账户类Account有方法:
    (1)getInfo(),返回String类型,返回卡的详细信息
    (2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
    (3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false

其中Customer类有姓名、身份证号、联系电话、家庭地址等属性
Customer类有方法say(),返回String类型,返回他的个人信息。

在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息

public class Account {
    private String cid;
    private double balance;
    private Customer customer;

    public Account() {
    }

    public Account(String cid, double balance, Customer customer) {
        this.cid = cid;
        this.balance = balance;
        this.customer = customer;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getInfo() {
        return "{" +
                "cid='" + cid + '\'' +
                ", balance=" + balance +
                ", customer=" + customer.say() +
                '}';
    }

    public boolean withdraw(double balance){
        if (this.balance>balance) {
            this.balance -= balance;
            return true;
        }else
            return false;

    }
    public boolean save(double balance){
        if (balance>0) {
            this.balance += balance;
            return true;
        }else
            return false;
    }
}
public class Customer {
    private String name;
    private String IDcard;
    private String phoneNumber;
    private String address;

    public Customer() {
    }

    public Customer(String name, String IDcard, String phoneNumber, String address) {
        this.name = name;
        this.IDcard = IDcard;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIDcard() {
        return IDcard;
    }

    public void setIDcard(String IDcard) {
        this.IDcard = IDcard;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    public String say() {
        return "{" +
                "name='" + name + '\'' +
                ", IDcard='" + IDcard + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
public class Bank {
    public static void main(String[] args) {
        Customer customer = new Customer("张三","721837128973","13212121212","中国");
        Account account = new Account("1001",2000,customer);
        account.save(10000);//存一万块
        account.withdraw(100);//取一百块
        System.out.println(account.getInfo());
    }
}
  1. 编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。
    (提供无参的构造器和一个有参的构造器)
public class BoxTest{
    public static void main(String[] args) {
        Box box = new Box(11,23,33);
        int volume = box.getVolume();
        System.out.println("该长方体的体积为"+volume);
    }
}

 class Box {
    private int length;

     public int getLength() {
         return length;
     }

     public void setLength(int length) {
         this.length = length;
     }

     public int getWidth() {
         return width;
     }

     public void setWidth(int width) {
         this.width = width;
     }

     public int getHeight() {
         return height;
     }

     public void setHeight(int height) {
         this.height = height;
     }

     private int width;
    private int height;

    public Box(int length, int width, int height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public Box() {
    }

    public int getVolume(){
        return length * width * height;
    }
}
  1. 定义一个圆类型
    提供显示圆周长功能的方法
    提供显示圆面积的方法
    提供无参的构造器和一个有参的构造器

  2. 设计一个Dog类,有名字、颜色和年龄属性,定义构造器初始化这些属性,定义输出方法show()显示其信息。

  3. 写一个人的类
    属性:名字,性别,年龄;提供无参的构造器和一个有参的构造器
    方法:(1)自我介绍的方法(2)吃饭的方法
    创建一个对象“张三”

  4. 写一个汽车类:
    属性:品牌;车长;颜色;价格;
    创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”
    提供无参的构造器和一个有参的构造器

  5. 写一个课程类:
    属性:课程名;学时;任课老师;
    创建五个对象:“c语言”,“java编程”,“php网络编程”,“c++”,“数据结构”
    提供无参的构造器和一个有参的构造器
    以上题目和第4题类似都是比较基础的题目

  6. 定义一个类,用于描述坐标点
    0——————>X
    |
    |
    | P(X,Y)
    |
    |
    Y

(1)具有计算当前点到原点距离的功能
(2)求到任意一点(m,n)的距离
(3)求到任意一点(Point p)的距离
(4)具有坐标点显示功能,显示格式(x,y)
(5)提供无参的构造器和一个有参的构造器

public class Point {
      public static void main(String[] args) {
            Point point = new Point(3,4);
            point.show();
            point.distanceOrigin();
            point.distance(1,2);
      }

      private double x;
      private double y;

      public Point() {
      }

      public Point(double x, double y) {
            this.x = x;
            this.y = y;
      }

      public void distanceOrigin(){
            System.out.println("到原点的坐标为"+Math.sqrt(x * x + y * y));
      }
      public void distance(double m , double n){
            System.out.println( "(" + x + "," + y + ")"+ "到" + "(" + m + "," + n + ")" + "的距离为"
                    +Math.sqrt((x-m)*(x-m)+(y-n)*(y-n)));
      }
      public void show(){
            System.out.println("当前坐标为"+"(" + x + "," + y + ")");
      }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值