Java exercises - day 16

Exercise 1: define the administrator class
The first step is to create a java project
Step 2 create Administrator.java

package com.hpe.java1;

public class Administrator {
	 String name;
	 String password;
	
	public void show(){
		System.out.println("Full name:"+name+",Password:"+password);
	}

	

}

Step 3 create InitialAdministrator.java

package com.hpe.java1;

public class AdministratorTest {
	public static void main(String[] args) {
		Administrator admin1 = new Administrator();
		Administrator admin2 = new Administrator();
		
		admin1.name="admin1";
		admin1.password="111111";
		admin1.show();
		
		admin2.name="admin2";
		admin2.password="222222";
		admin2.show();
	}
	

}

Run InitialAdministrator.java and the result is:

Exercise 2: define customer classes
The first step is to create a java project
Step 2 create Customer.java

package com.hpe.java1;

public class Customer {
	int points;
	String cardType;
	public void show(){
		System.out.println("Integral:"+points+",Card type:"+cardType);
	}

}

Customer.java

Step 3 InitialCustomer.java

package com.hpe.java1;

public class InitialCustomer {
	public static void main(String[] args) {
		Customer c= new Customer();
		c.points=1000;
		c.cardType="Ordinary card";
		c.show();
		
	}

}

Run InitialCustomer.java with the result


Exercise 3: create an administrator object
The first step is to create a java project
Step 2: create AdministratorTest.java
AdministratorTest.java

package com.hpe.java1;

public class AdministratorTest {
	public static void main(String[] args) {
		Administrator admin1 = new Administrator();
		Administrator admin2 = new Administrator();
		
		admin1.name="admin1";
		admin1.password="111111";
		admin1.show();
		
		admin2.name="admin2";
		admin2.password="222222";
		admin2.show();
	}
	

}

The result after operation is

Exercise 4: change the administrator password
The first step is to create a java project
Step 2 create ChangePassword.java

package com.hpe.java1;

import java.util.Scanner;

public class ChangePassword {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Administrator admin = new Administrator();
		admin.name="admin1";
		admin.password="111111";
		System.out.print("Please enter the user name:");
		String nameInput= sc.next();
		System.out.print("Please input a password:");
		String pwd= sc.next();
		if(admin.name.equals(nameInput)&&admin.password.equals(pwd)){
			System.out.println("Please enter a new password:");
			admin.password=sc.next();
			System.out.println("The password was modified successfully. The new password is:"+admin.password);
			
		}else{
			System.out.println("User name and password do not match, no access");
		}
	}

}

The result after operation is

Exercise 5: define the car category
According to the class about Nicholas Cage's sports car, implement the car class and create cage's car object.

1.Car

package com.hpe.java1;

public class Car {
	private String brand;
	private String xingHao;
	private String color;
	private int price;
	public Car(String brand, String xingHao, String color, int price) {
		super();
		this.brand = brand;
		this.xingHao = xingHao;
		this.color = color;
		this.price = price;
	}
	public Car() {
		super();
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getXingHao() {
		return xingHao;
	}
	public void setXingHao(String xingHao) {
		this.xingHao = xingHao;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public void start(){
		System.out.println("Engine started");
	}
	public void stop(){
		System.out.println("The engine has stopped");
	}
	public void speed(){
		System.out.println("Acceleration");
	}

}

2.CarTest

package com.hpe.java1;

public class CarTest {
	public static void main(String[] args) {
		Car car = new Car("Ferrari", "F360 Spider", "yellow", 380);
		car.start();
		car.speed();
		car.stop();
	}

}

Exercise 6: defining warships
According to the following class diagram and object diagram, use java code to define "Chinese warship class", and create destroyers, cruisers and other objects.

1.Cruiser

package com.hpe.java;

public class Cruiser extends Warship{
	public Cruiser(String brand, String name, String funCation, int disPlacement, int length, String suJection){
		super(brand,name,funCation,disPlacement,length,suJection);
	}
	public void show(){
		super.showBrand();
		super.showDisPlacement();
		super.showFunCation();
		super.showLength();
		super.showSuJection();
	}


}

2.Destroyer

package com.hpe.java;

public class Destroyer extends Warship{
	public Destroyer(String brand, String name, String funCation, int disPlacement, int length, String suJection){
		super(brand,name,funCation,disPlacement,length,suJection);
	}
	public void show(){
		super.showBrand();
		super.showDisPlacement();
		super.showFunCation();
		super.showLength();
		super.showSuJection();
	}

}

3.Test

package com.hpe.java;

public class Test {
	
	public static void main(String[] args) {
		Destroyer d  = new Destroyer("052D", "Shandong ship", "air defense", 7000, 150, "Beihai fleet");
		Cruiser c = new Cruiser("052D", "Liaoning ship", "aircraft carrier", 80000, 900, "Beihai fleet");
		d.show();
		System.out.println("======================================");
		c.show();
	}

}

4.Warship

package com.hpe.java;

public class Warship {
	private String brand;//Model
	private String name;//Name
	private String funCation;//function
	private int disPlacement;//Displacement
	private int length;
	private String suJection;//subjection
	public Warship(String brand, String name, String funCation, int disPlacement, int length, String suJection) {
		super();
		this.brand = brand;
		this.name = name;
		this.funCation = funCation;
		this.disPlacement = disPlacement;
		this.length = length;
		this.suJection = suJection;
	}
	public Warship() {
		super();
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFunCation() {
		return funCation;
	}
	public void setFunCation(String funCation) {
		this.funCation = funCation;
	}
	public int getDisPlacement() {
		return disPlacement;
	}
	public void setDisPlacement(int disPlacement) {
		this.disPlacement = disPlacement;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getSuJection() {
		return suJection;
	}
	public void setSuJection(String suJection) {
		this.suJection = suJection;
	}
	public void showBrand(){
		System.out.println("Model:"+this.getBrand());
	}
	
	public void showDisPlacement(){
		System.out.println("Displacement:"+this.getDisPlacement());
	}
	
	public void showLength(){
		System.out.println("Length:"+this.getLength());
	}
	
	public void showFunCation(){
		System.out.println("Attack performance:"+this.getFunCation());
	}
	
	public void showSuJection(){
		System.out.println("Subjection:"+this.getSuJection());
	}

	

}

Exercise 7: abstract the object in the event and define the class.
Steps:
1. Abstract entities in the real world as objects;
2. Analyze the properties and behaviors of this object.

Event description:
On August 8, Beijing time, the men's weightlifting 56 kg competition of 2016 Rio Olympic Games was held in the No. 2 hall of Rio Convention and Exhibition Center. Chinese player long Qingquan grabbed 137 kg and jerked 170 kg, with a total score of 307 kg, won the championship.
At 3:00 a.m. Beijing time on August 8, 2016, in the women's synchronized 3M board competition of Rio Olympic Games, Chinese combination Wu Minxia / Shi Tingmao led the way and finally won the championship with a total score of 345.60 points.
[China's first gold! Zhang Mengxue won the gold medal of women's 10 meter air pistol! ]In the final of the women's 10m air pistol, Zhang Mengxue, who took part in the Olympic Games for the first time, played an outstanding role, hitting more than 10 rings in succession! Finally, he won the gold medal with 199.4 rings and won his first Olympic gold medal.
1.airGun

package com.hpe.java2;

public class airGun extends Og{
	private String name;
	private String nation;
	private String score;
	public airGun(String date, String pot, String projec, String name, String nation, String score) {
		super(date, pot, projec);
		this.name = name;
		this.nation = nation;
		this.score = score;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	
	public void show(){
		
		System.out.println(this.getNation()+"Player"+this.getName()+
				"Total score"+this.getScore()+"Get"+this.getDate()+this.getPot()+this.getProjec()+"Champion");
	}

}

2.Diving

package com.hpe.java2;

public class Diving extends Og{
	private String name;
	private String nation;
	private String score;
	public Diving(String date, String pot, String projec, String name, String nation, String score) {
		super(date, pot, projec);
		this.name = name;
		this.nation = nation;
		this.score = score;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	
	public void show(){
		
		System.out.println(this.getNation()+"Player"+this.getName()+
				"Total score"+this.getScore()+"Get"+this.getDate()+this.getPot()+this.getProjec()+"Champion");
	}

}

3.Og

package com.hpe.java2;

public class Og {
	private String date;
	private String pot;
	private String projec;
	public Og(String date, String pot, String projec) {
		super();
		this.date = date;
		this.pot = pot;
		this.projec = projec;
	}
	public Og() {
		super();
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getPot() {
		return pot;
	}
	public void setPot(String pot) {
		this.pot = pot;
	}
	public String getProjec() {
		return projec;
	}
	public void setProjec(String projec) {
		this.projec = projec;
	}
	

}

4.Test

package com.hpe.java2;

public class Test {
	public static void main(String[] args) {
		Weight w= new Weight("2016 year", "Rio", "Men's 56kg weightlifting", "Long Qingquan", "China", "307 Kg.");
		w.show();
		System.out.println("================================================================");
		Diving d = new Diving("2016 year", "Rio", "Women's double 3M board", "Wu Minxia/Shi Ting Mao", "China", "345.60 branch");
		d.show();
		System.out.println("================================================================");
		airGun a= new airGun("2016 year", "Rio", "Women's 10 meter air pistol", "Zhang Meng Xue", "China", "199.4 ring");
		a.show();
	}

}

5.Weight

package com.hpe.java2;

public class Weight extends Og{
	private String name;
	private String nation;
	private String score;
	public Weight(String date, String pot, String projec, String name, String nation, String score) {
		super(date, pot, projec);
		this.name = name;
		this.nation = nation;
		this.score = score;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	
	public void show(){
		
		System.out.println(this.getNation()+"Player"+this.getName()+
				"Total score"+this.getScore()+"Get"+this.getDate()+this.getPot()+this.getProjec()+"Champion");
	}

}

Published 25 original articles, won praise 1, visited 151
Private letter follow

Tags: Java

Posted on Tue, 14 Jan 2020 04:16:34 -0500 by argrafic