Java exercise - 08

Posted by cupboy on Fri, 17 Dec 2021 02:40:54 +0100

1. Requirement Description: implementation idea of encapsulating member variables of webworm package, Superman package and chat package: 1 Add an access modifier to the attribute; 2. Create setter s and getter s.

import java.util.Scanner;
//Internet worm package
public class WangChong {
	private String name;
	private int price;
	private int time ;
	private int liuliang;

	public WangChong() {
		
	}
	
	public WangChong(String name,int price,int time , int liuliang) {
		this.name = name;
		this.price = price;
		this.time = time;
		this.liuliang = liuliang;
	}
	
	public String getName() {
		return name;
	}

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

	public int getPrice() {
		return price;
	}

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

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public int getLiuliang() {
		return liuliang;
	}

	public void setLiuliang(int liuliang) {
		this.liuliang = liuliang;
	}
	public void message() {
		System.out.println("The package information is as follows:");
		System.out.println("Package name:"+this.name);
		System.out.println("Price:"+this.price+"element");
		System.out.println("term of validity:"+this.time+"day");
		System.out.println("Residual flow:"+this.liuliang+"MB");
		
		Scanner sc = new Scanner(System.in);
		System.out.print("\n"+"Please enter the traffic you want to use:");
		int liuliang1 = sc.nextInt();
		if(liuliang1 <=this.liuliang) {
			System.out.println("Enough flow");
		}else {
			System.out.println("Insufficient flow");
		}
		
		System.out.print("\n"+"Please enter the number of days you want to use:");
		int day = sc.nextInt();
		if(day <=this.time) {
			System.out.println("Within the scope of use");
		}else {
			System.out.println("Out of use");
		}
	}
	
}
import java.util.Scanner;
//Superman package
public class ChaoRen {
	private String name ;
	private int price ;
	private int time;
	private int liuliang;

	public ChaoRen() {
		
	}
	public ChaoRen(String name,int price,int time , int liuliang) {
		this.name = name;
		this.price = price;
		this.time = time;
		this.liuliang = liuliang;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public int getLiuliang() {
		return liuliang;
	}
	public void setLiuliang(int liuliang) {
		this.liuliang = liuliang;
	}
	public void message() {
		System.out.println("The package information is as follows:");
		System.out.println("Package name:"+this.name);
		System.out.println("Price:"+this.price+"element");
		System.out.println("term of validity:"+this.time+"day");
		System.out.println("Residual flow:"+this.liuliang+"MB");
		
		Scanner sc = new Scanner(System.in);
		System.out.print("\n"+"Please enter the traffic you want to use:");
		int liuliang1 = sc.nextInt();
		if(liuliang1 <=this.liuliang) {
			System.out.println("Enough flow");
		}else {
			System.out.println("Insufficient flow");
		}
		
		System.out.print("\n"+"Please enter the number of days you want to use:");
		int day = sc.nextInt();
		if(day <=this.time) {
			System.out.println("Within the scope of use");
		}else {
			System.out.println("Out of use");
		}
	}
}
import java.util.Scanner;
//Tuberculosis package
public class HuaLao {
	private String name ;
	private int price ;
	private int time ;
	private int liuliang;

	public HuaLao() {
		
	}
	public HuaLao(String name,int price,int time , int liuliang) {
		this.name = name;
		this.price = price;
		this.time = time;
		this.liuliang = liuliang;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public int getLiuliang() {
		return liuliang;
	}
	public void setLiuliang(int liuliang) {
		this.liuliang = liuliang;
	}
	public void message() {
		System.out.println("The package information is as follows:");
		System.out.println("Package name:"+this.name);
		System.out.println("Price:"+this.price+"element");
		System.out.println("term of validity:"+this.time+"day");
		System.out.println("Residual flow:"+this.liuliang+"MB");
		
		Scanner sc = new Scanner(System.in);
		System.out.print("\n"+"Please enter the traffic you want to use:");
		int liuliang1 = sc.nextInt();
		if(liuliang1 <=this.liuliang) {
			System.out.println("Enough flow");
		}else {
			System.out.println("Insufficient flow");
		}
		
		System.out.print("\n"+"Please enter the number of days you want to use:");
		int day = sc.nextInt();
		if(day <=this.time) {
			System.out.println("Within the scope of use");
		}else {
			System.out.println("Out of use");
		}
	}
	}
	
Please enter the name of the package you want to query: webworm package
 The package information is as follows:
Package name:Internet worm package
 Price: 19 yuan
 Validity: 100 days
 Residual flow: 1000 MB

Please enter the traffic you want to use: 100
 Enough flow

Please enter the number of days you want to use: 30
 Within the scope of use

2. Write a program to describe the dog, and use the object-oriented idea to write user-defined information to describe the dog. Setting attributes include: variety, age, mood and name; Methods include: call, run

requirement:

1. Set the private access permission of the property, and access the property through the public get and set methods

2. The limited mood can only be "in a good mood" and "in a bad mood". If the input is invalid, the default setting is "in a good mood".

3. Set the constructor to assign a value to a property

4. The method of calling and running needs to describe different ways of behavior according to the mood.

5. Write test classes, test dog objects and related methods (test data information customization)

import java.util.Scanner;

public class Dog {
	private String type;
	private int age;
	private String mood;
	private String name;
	
	public Dog() {
		
	}
	
	public Dog(String type,int age, String mood,String name) {
		this.type = type;
		this.age = age;
		this.mood = mood;
		this.name = name;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getAge() {
		return age;
	}

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

	public String getMood() {
		return mood;
	}

	public void setMood(String mood) {
		this.mood = mood;
	}

	public String getName() {
		return name;
	}

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



	public void jiao() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the dog's mood:");
		String mood1 = sc.next();
		if(mood1.equals("in a good humor")) {
			System.out.println("dog's bark");
		}else if(mood1.equals("in bad mood")){
			System.out.println("Dogs don't bark");
		}else {
			System.out.println("Incorrect input");
		}
		
	}
	
	public void run() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the dog's mood:");
		String mood1 = sc.next();
		if(mood1.equals("in a good humor")) {
			System.out.println("Dog run");
		}else if(mood1.equals("in bad mood")){
			System.out.println("Dogs don't run");
		}else {
			System.out.println("Incorrect input");
		}
	}
	
	

}
public class TestDog {

	public static void main(String[] args) {
		Dog dog = new Dog("Golden hair",2,"in a good humor","Wang Xiaowang");
		System.out.println("Breed of dog:"+dog.getType());
		System.out.println("Dog age:"+dog.getAge());
		System.out.println("Dog mood:"+dog.getMood());
		System.out.println("Dog name:"+dog.getName());
		dog.jiao();
		dog.run();

	}
}
Dog breed: golden hair
 Dog age: 2
 Dog's mood: in a good mood
 Dog's name: Wang Xiaowang
 Please enter the dog's mood: good mood
 dog's bark
 Please enter the dog's mood: good mood
 Dog run

3. Write programs to describe IT practitioners, and write custom classes to describe IT practitioners with the idea of object-oriented. Setting attributes include: name, age, technical direction and working years; Methods include: working

requirement:

1. Set the private access permission of the property, and access the property through the public get and set methods

2. IT employees must be at least 15 years old. Invalid information needs to be prompted, and the default age is 15.

3. Work method receives the work unit and job through input parameters, and outputs personal work information

4. Write test classes, test IT practitioner class objects and related methods (test data information customization)

import java.util.Scanner;

public class ITperson {
	private String name;
	private int age = 15;
	private String technology;
	private String time;
	
	public ITperson() {
		
	}
	
	public ITperson(String name,int age,String technology,String time) {
		this.name = name;
		this.age = age;
		this.technology = technology;
		this.time = time;
	
	}
	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 getTechnology() {
		return technology;
	}
	public void setTechnology(String technology) {
		this.technology = technology;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	
	
	public void work(String danwei,String zhiwei){
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter IT Age of practitioner:");
		int age1 = sc.nextInt();
		if(age1<15) {
			System.out.println("Invalid information, IT The practitioner must be at least 15 years old");

		}else {
			System.out.println("Personal information is as follows:");
			System.out.println("full name:"+name);
			System.out.println("Age:"+age);
			System.out.println("Technical direction:"+technology);
			System.out.println("Working years:"+time);
			System.out.println("Work unit:"+danwei);
			System.out.println("post:"+zhiwei);
		}
		
	}
}
Please enter IT Age of practitioner: 23
 Personal information is as follows:
Name: Wang Zhangsan
 Age: 23
 Technical direction: back end development
 Working years: 5 years
 Work unit:R & D department
 post:manager

4. Write a program to describe the truck information. If a company wants to develop "X taxi company vehicle management system", please design the truck class with object-oriented idea. Setting: attributes: license plate number, vehicle type, color, daily rent and load capacity. Method: lease

requirement:

1. Set the private access permission of the property, and access the property through the public get and set methods

2. The lease method receives the name of the car renter and the lease time by inputting parameters, describes the lease status, and requires to judge the validity of the lease time.

3. Design constructor to implement attribute assignment

4. Write test classes, test truck class objects and related methods (test data information customization)

public class Car {
	private String number;
	private String model;
	private String color;
	private int money;
	private int weight;
	
	public Car() {
		
	}
	
	public Car(String number,String model,String color,int money,int weight) {
		this.number = number;
		this.model = model;
		this.color = color;
		this.money = money;
		this.weight = weight;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	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 int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	
	public void rent(String peopleName,int time) {
		if(time>30) {
			System.out.println("Car rental succeeded!");
			System.out.println("license plate number:"+number);
			System.out.println("Model:"+model);
			System.out.println("Color:"+color);
			System.out.println("Daily rent:"+money+"element");
			System.out.println("Load capacity:"+weight+"KG");
			System.out.println("Name of car renter:"+peopleName);
			System.out.println("Rental time:"+time+"minute");
		}else {
			System.out.println("Car rental failed!");
			System.out.println("Car rental time must be greater than 30 minutes");
		}
		
	}
	
	

}
Taxi company vehicle management system
 Car rental failed!
Car rental time must be greater than 30 minutes

Topics: Java Eclipse