Java类练习题大全

目录

1.创建一个名为Person的类,该类包含以下属性:姓名、年龄、性别和创建一个对象并打印出其属性值。

2.创建一个名为Rectangle的类,该类包含以下属性:长、宽。创建一个对象并计算其面积和周长。

3.创建一个名为Student的类,该类包含以下属性:姓名、年龄、性别、学号、班级、成绩。创建一个对象并打印出其属性值。

4.创建一个名为Car的类,该类包含以下属性:品牌、型号、颜色、价格。创建两个对象并比较它们的价格。

5.创建一个名为Employee的类,该类包含以下属性:姓名、年龄、性别、工号、职位、薪水。创建一个对象并打印出其属性值。

6.创建一个名为Dog的类,该类包含以下属性:姓名、品种、年龄、颜色。创建一个对象并实现以下操作:叫声、吃饭、睡觉。

7.创建一个名为Book的类,该类包含以下属性:书名、作者、出版社、价格。创建一个对象并实现以下操作:借书、还书、查询是否可借。

8.创建一个名为Bank的类,该类包含以下属性:银行名称、地址、电话。创建一个对象并打印出其属性值。

9.创建一个名为Date的类,该类包含以下属性:年、月、日。创建一个对象并实现以下操作:计算两个日期之间的天数、判断是否为闰年。    

10.创建一个名为Triangle的类,该类包含以下属性:三边长。创建一个对象并计算其面积和周长。

11.创建一个名为Point的类,该类包含以下属性:x坐标、y坐标。创建两个对象并计算它们之间的距离。

12.创建一个名为Phone的类,该类包含以下属性:品牌、型号、价格。创建一个对象并实现以下操作:打电话、发短信、查询余额。

13.创建一个名为Rectangle的类,该类包含以下属性:左上角坐标、右下角坐标。创建一个对象并计算其面积和周长。

14.创建一个名为BankCard的类,该类包含以下属性:卡号、持卡人姓名、余额、密码。创建一个对象并实现以下操作:存款、取款、查询余额。

15.创建一个名为Person的类,该类包含以下属性:姓名、年龄、性别、身高、体重。创建一个对象并实现以下操作:计算BMI指数、判断是否健康。

16创建一个名为Animal的类,该类包含以下属性:种类、年龄、颜色。创建一个对象并实现以下操作:叫声、吃东西、睡觉。


1.创建一个名为Person的类,该类包含以下属性:姓名、年龄、性别和创建一个对象并打印出其属性值。

public class Example1 {
	public static void main(String[] args) {
     Person person=new Person("张三","男",15);
     person.print();
	}
}

class Person {
	private String name, sex;
	private int age;

	public Person(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	public  void print() {
		System.out.println("name="+name+",age="+age+",sex="+sex);
	}
}

2.创建一个名为Rectangle的类,该类包含以下属性:长、宽。创建一个对象并计算其面积和周长。

public class Example2 {
	public static void main(String[] args) {
		Rectangle rectangle = new Rectangle(5, 10);
	    double area = rectangle.getArea();
		double perimeter = rectangle.getPerimeter();
		System.out.println("area: " + area);
		System.out.println("perimeter: " + perimeter);
	}
}
class Rectangle{
	private double length,width;
	
	public Rectangle(double length, double width) {
		super();
		this.length = length;
		this.width = width;
	}

	public double getLength() {
		return length;
	}

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

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}
	 public double getArea() {
	        return length * width;
	    }

	    public double getPerimeter() {
	        return 2 * (length + width);
	    }
}

3.创建一个名为Student的类,该类包含以下属性:姓名、年龄、性别、学号、班级、成绩。创建一个对象并打印出其属性值。

public class Student {
	public static void main(String[] args) {
		Student student = new Student("张三",18, "男", "20210001", "计算机科学与技术", 90.5f);
	student.print();
	}
    private String name;
    private int age;
    private String gender;
    private String stunum;
    private String clazz;
    private float score;

    public Student(String name, int age, String gender, String stunum, String clazz, float score) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.stunum = stunum;
        this.clazz = clazz;
        this.score = score;
    }

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

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

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

    public void setStunum(String stunum) {
        this.stunum = stunum;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public String getStunum() {
        return stunum;
    }

    public String getClazz() {
        return clazz;
    }

    public float getScore() {
        return score;
    }
    public  void print() {
        Student student = new Student("张三",18, "男", "20210001", "计算机科学与技术", 90.5f);
    	System.out.println("姓名:" + student.getName());
    	System.out.println("年龄:" + student.getAge());
    	System.out.println("性别:" + student.getGender());
    	System.out.println("学号:" + student.getStunum());
    	System.out.println("班级:" + student.getClazz());
    	System.out.println("成绩:" + student.getScore());
	}
}

4.创建一个名为Car的类,该类包含以下属性:品牌、型号、颜色、价格。创建两个对象并比较它们的价格。

public class Car {
	
    private String brand;
    private String model;
    private String color;
    private double price;

    public Car() {}

    public Car(String brand, String model, String color, double price) {
        this.brand = brand;
        this.model = model;
        this.color = color;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", model='" + model + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
    public static void main(String[] args) {
        // 实例化两个对象
        Car car1 = new Car("Toyota", "Camry", "Red", 20000);
        Car car2 = new Car("Honda", "Accord", "Blue", 25000);
    
        if (car1.getPrice() > car2.getPrice()) {
            System.out.println("Car1 is more expensive than Car2.");
        } else if (car1.getPrice() < car2.getPrice()) {
            System.out.println("Car2 is more expensive than Car1.");
        } else {
            System.out.println("Car1 and Car2 have the same price.");
        }
    }
}

5.创建一个名为Employee的类,该类包含以下属性:姓名、年龄、性别、工号、职位、薪水。创建一个对象并打印出其属性值。

public class Example5 {
	public static void main(String[] args) {
    	Employee employee = new Employee("张三", 25, "男", 1001, "经理", 10000.0);
    	Employee.print();
	}
}
class Employee {
    private String name;
    private int age;
    private String gender;
    private int id;
    private String position;
    private double salary;

    public Employee(String name, int age, String gender, int id, String position, double salary) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.id = id;
        this.position = position;
        this.salary = salary;
    }

    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 String getGender() {
        return gender;
    }

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

    public int getId() {
        return id;
    }

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

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public  void print() {
    System.out.println("name="+name+",age="+age+",gender="+gender+",id="+id+
    			",position="+position+",salary="+salary);
    			
       }
}

6.创建一个名为Dog的类,该类包含以下属性:姓名、品种、年龄、颜色。创建一个对象并实现以下操作:叫声、吃饭、睡觉。

public class Dog {
	public static void main(String[] args) {
		Dog dog = new Dog("小白", "哈士奇", 2, "白色");
	    dog.bark();
		dog.eat();
		dog.sleep();
	}
   private String name;
   private String breed;
   private int age;
   private String color;

    public Dog(String name, String breed, int age, String color) {
	super();
	this.name = name;
	this.breed = breed;
	this.age = age;
	this.color = color;
}

	public String getName() {
		return name;
	}

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

	public String getBreed() {
		return breed;
	}

	public void setBreed(String breed) {
		this.breed = breed;
	}

	public int getAge() {
		return age;
	}

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

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public void bark() {
        System.out.println("汪汪汪!");
    }

    public void eat() {
        System.out.println("狗狗正在吃饭。");
    }

    public void sleep() {
        System.out.println("狗狗正在睡觉。");
    }
}

7.创建一个名为Book的类,该类包含以下属性:书名、作者、出版社、价格。创建一个对象并实现以下操作:借书、还书、查询是否可借。

public class Example7 {
		public static void main(String[] args) {
		        Book book = new Book("Java", "Black", "清华大学出版社", 98.00);
		        book.canBorrow(); 
		        book.borrow(); 
		        book.canBorrow(); 
		        book.returnBook();
		        book.canBorrow(); 
		}
	}
class Book {
 private String Bookname; 
 private String author; 
 private String publisher;
 private double price;
 private boolean borrowed; 
 public Book(String Bookname, String author, String publisher, double price) {
     this.Bookname = Bookname;
     this.author = author;
     this.publisher = publisher;
     this.price = price;
     this.borrowed = false; 
 }
 public String getBookname() {
     return Bookname;
 }

 public String getAuthor() {
     return author;
 }

 public String getPublisher() {
     return publisher;
 }

 public double getPrice() {
     return price;
 }

 public boolean isBorrowed() {
     return borrowed;
 }
 public void borrow() {
     if (borrowed) {
         System.out.println("这本书已经被借出了!");
     } else {
         borrowed = true;
         System.out.println("借书成功!");
     }
 }
 public void returnBook() {
     if (borrowed) {
         borrowed = false;
         System.out.println("还书成功!");
     } else {
         System.out.println("这本书没有被借出,无需归还!");
     }
 }
 public void canBorrow() {
     if (borrowed) {
         System.out.println("这本书已经被借出了,不可借!");
     } else {
         System.out.println("这本书可以借阅!");
     }
 }
}

8.创建一个名为Bank的类,该类包含以下属性:银行名称、地址、电话。创建一个对象并打印出其属性值。

public class Example8 {
public static void main(String[] args) {
	 Bank bank = new Bank("中国银行", "北京市朝阳区建国门外大街1号", "95566");
	 System.out.println("银行名称:" + bank.getName());
	 System.out.println("地址:" + bank.getAddress());
	 System.out.println("电话:" + bank.getPhone());

}
}
class Bank{
	private String name;
    private String address;
    private String phone;
	public Bank(String name, String address, String phone) {
		super();
		this.name = name;
		this.address = address;
		this.phone = phone;
	}
	public String getName() {
		return name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public String getPhone() {
		return phone;
	}
}

9创建一个名为Date的类,该类包含以下属性:年、月、日。创建一个对象并实现以下操作:计算两个日期之间的天数、判断是否为闰年。    

public class Example9 {
	public static void main(String[] args) {
		 Date date1 = new Date(2021, 5, 1);
	     Date date2 = new Date(2021, 5, 10);
	     int days = date1.daysBetween(date2);
	     System.out.println("两个日期之间的天数为:" + days);

	     Date date3 = new Date(2020, 1, 1);
	     boolean isLeapYear = date3.isLeapYear();
	     System.out.println("2020年是否为闰年:" + isLeapYear);
		
	}

}
class Date{
	private int year,month,day;

	public Date(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}
	public int daysBetween(Date date) {
	     int days = 0;
	     if (this.year == date.getYear()) {
	         if (this.month == date.getMonth()) {
	             days = Math.abs(this.day - date.getDay());
	         } else {
	             int days1 = getDaysOfMonth(this.year, this.month) - this.day;
	             int days2 = date.getDay();
	             for (int i = this.month + 1; i < date.getMonth(); i++) {
	                 days += getDaysOfMonth(this.year, i);
	             }
	             days += days1 + days2;
	         }
	     } else {
	         Date start, end;
	         if (this.year < date.getYear()) {
	             start = this;
	             end = date;
	         } else {
	             start = date;
	             end = this;
	         }
	         int days1 = getDaysOfMonth(start.getYear(), start.getMonth()) - start.getDay();
	         int days2 = end.getDay();
	         for (int i = start.getMonth() + 1; i <= 12; i++) {
	             days += getDaysOfMonth(start.getYear(), i);
	         }
	         for (int i = start.getYear() + 1; i < end.getYear(); i++) {
	             days += isLeapYear(i) ? 366 : 365;
	         }
	         for (int i = 1; i < end.getMonth(); i++) {
	             days += getDaysOfMonth(end.getYear(), i);
	         }
	         days += days1 + days2;
	     }
	     return days;
	 }
	 public boolean isLeapYear() {
	     return isLeapYear(this.year);
	 }

	 private boolean isLeapYear(int year) {
	     return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	 }

	 private int getDaysOfMonth(int year, int month) {
	     int days;
	     switch (month) {
	         case 2:
	             days = isLeapYear(year) ? 29 : 28;
	             break;
	         case 4:
	         case 6:
	         case 9:
	         case 11:
	             days = 30;
	             break;
	         default:
	             days = 31;
	     }
	     return days;
	 }
	}

10.创建一个名为Triangle的类,该类包含以下属性:三边长。创建一个对象并计算其面积和周长。

public class Example10 {
	public static void main(String[] args) {
		Triangle triangle = new Triangle(3.0, 4.0, 5.0);
		double perimeter = triangle.getPerimeter();
		double area = triangle.getArea();
		System.out.println("Perimeter: " + perimeter);
		System.out.println("Area: " + area);
	}

}
class Triangle {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle() {}

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    public double getArea() {
        double s = (side1 + side2 + side3) / 2;
        return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
    }
}

11.创建一个名为Point的类,该类包含以下属性:x坐标、y坐标。创建两个对象并计算它们之间的距离。

public class Example11 {
	public static void main(String[] args) {
		 Point p1 = new Point(0, 0);
	        Point p2 = new Point(3, 4);
	        double distance = p1.distance(p2);
	        System.out.println("The distance between p1 and p2 is " + distance);	
	}
}
class Point {
    private int x;
    private int y;

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

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public double distance(Point other) {
        int dx = this.x - other.getX();
        int dy = this.y - other.getY();
        return Math.sqrt(dx*dx + dy*dy);
    }
}

12.创建一个名为Phone的类,该类包含以下属性:品牌、型号、价格。创建一个对象并实现以下操作:打电话、发短信、查询余额。

public class Example12 {
	public static void main(String[] args) {
		Phone phone = new Phone ("Apple", "iPhone 12", 7999.0);
		phone.call("13212660403");
		phone.sendMessage("Hello World!");
		phone.setBalance(100.0);
		phone.checkBalance();

	}

}
class Phone{
private String brand;
private String type;
private double price;
private double balance;

public Phone(String brand, String type, double price){
   this.brand = brand;
   this.type = type;
   this.price = price;
   this.balance = 0;
}

public void call(String number){
   System.out.println("正在拨打电话:" + number);
}

public void sendMessage(String message){
   System.out.println("正在发送短信:" + message);
}

public void checkBalance(){
   System.out.println("当前余额为:" + balance);
}

public String getBrand(){
   return brand;
}

public String getType(){
   return type;
}

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

13.创建一个名为Rectangle的类,该类包含以下属性:左上角坐标、右下角坐标。创建一个对象并计算其面积和周长。

public class Example13 {
public static void main(String[] args) {
	Rectangle rectangle = new Rectangle(0, 0, 6, 10);
	int area = rectangle.getArea();
	int perimeter = rectangle.getPerimeter();
	System.out.println("area: " + area);
	System.out.println("perimeter: " + perimeter);
}
}
class Rectangle {
    private int x1, y1, x2, y2;

    public Rectangle(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public int getArea() {
        int width = Math.abs(x2 - x1);
        int height = Math.abs(y2 - y1);
        return width * height;
    }

    public int getPerimeter() {
    	  int width = Math.abs(x2 - x1);
          int height = Math.abs(y2 - y1);
          return 2 * (width + height);
      }
  }

14.创建一个名为BankCard的类,该类包含以下属性:卡号、持卡人姓名、余额、密码。创建一个对象并实现以下操作:存款、取款、查询余额。

public class Example14 {
public static void main(String[] args) {
	BankCard bankcard = new BankCard("123456789", "张三", 1000, "123456");

	bankcard.deposit(500); //存款
	bankcard.checkBalance(); //查询余额
	bankcard.withdraw(200, "123456"); //取款200元,输入正确密码
	bankcard.withdraw(1000, "123456"); //取款1000元,余额不足
	bankcard.withdraw(200, "111111"); //取款200元,输入错误密码

}
}
class BankCard {
    private double balance; //余额
    private String password;            //持卡人姓名cardHolderName
    public BankCard(String cardNumber, String cardHolderName, double balance, String password) {
        this.balance = balance;
        this.password = password;
    }
    public void deposit(double amount) {
        balance += amount;
        System.out.println("成功存入" + amount + "元,当前余额为" + balance + "元。");
    }
    public void withdraw(double amount, String password) {
        if (password.equals(this.password)) {
            if (amount <= balance) {
                balance -= amount;
                System.out.println("成功取出" + amount + "元,当前余额为" + balance + "元。");
            } else {
                System.out.println("余额不足,无法取款。");
            }
        } else {
            System.out.println("密码错误,无法取款。");
        }
    }
    public void checkBalance() {
        System.out.println("当前余额为" + balance + "元。");
    }
}

15.创建一个名为Person的类,该类包含以下属性:姓名、年龄、性别、身高、体重。创建一个对象并实现以下操作:计算BMI指数、判断是否健康。

public class Example15 {
	public static void main(String[] args) {
		Person person = new Person();
        person.name = "张三";
        person.age = 18;
        person.sex = '男';
        person.height = 1.8;
        person.weight = 80;
        System.out.println(person.name + "的BMI指数为:" + person.getBMI());
        System.out.println(person.name + "的健康状况为:" + person.isHealthy());

	}

}
class Person {
    public String name;
    public int age;
    public char sex;
    public double height;
    public double weight;

    public double getBMI() {
        return weight / (height * height);
    }

    public String isHealthy() {
        double bmi = getBMI();
        if (bmi < 18.5) {
            return "偏瘦";
        } else if (bmi >= 18.5 && bmi <= 24.9) {
            return "正常";
        } else {
            return "偏胖";
        }
    }
}

16创建一个名为Animal的类,该类包含以下属性:种类、年龄、颜色。创建一个对象并实现以下操作:叫声、吃东西、睡觉。

public class Example16 {
public static void main(String[] args) {
	Animal animal = new Animal("dog", 3, "brown");
	animal.makeSound();
	animal.eat();
	animal.sleep();

}
}
class Animal {
    private String species;
    private int age;
    private String color;

    public Animal(String species, int age, String color) {
        this.species = species;
        this.age = age;
        this.color = color;
    }

    public void makeSound() {
        System.out.println("Animal is making sound.");
    }

    public void eat() {
        System.out.println("Animal is eating.");
    }

    public void sleep() {
        System.out.println("Animal is sleeping.");
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public int getAge() {
        return age;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简而言之希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值