JAVA day05: Code exercise (Methods of classes and objects)

Posted by roninblade on Tue, 04 Jan 2022 07:06:34 +0100

Topic 1: definition and application of method

Training points:
Properties of class
Class method
Requirement description
Write telephone cards

Implementation idea:
1. Define mobile card
2. Define its attributes and methods

public class MobileCard {
	public String num;//Card number
	public String name;//user name
	public String password;//password
	public String packageName;//Package name
	public double monthlyConsumption;//Consumption amount of the current month
	public double accountBalance ;//Account balance
	public double actualCallDuration;//Actual call duration
	public int numberOfMessages;//Actual number of SMS messages sent
	public double internetFlow;//Actual Internet traffic
	
	//Display phone card information
	public void messageCard(){
		System.out.println("Card number:"+num);
		System.out.println("The user name is:"+name);
		System.out.println("Password is:"+password);
		System.out.println("Package name:"+packageName);
		System.out.println("The consumption amount of the current month is:"+monthlyConsumption);
		System.out.println("The account balance is:"+accountBalance);
		System.out.println("Actual call duration:"+actualCallDuration);
		System.out.println("The actual number of SMS sent is:"+numberOfMessages);
		System.out.println("The actual online traffic is:"+internetFlow);
	}
}

Topic 2: application of method call

Training points:
Object creation
Calls to properties and methods

Requirements Description:
Create phone card object
Initialize the object and output information

Implementation idea:
1. Create a phone card object
2. Call member variables
3. Call member method

//class
public class MobileCard2 {
	public String num="18888888";//Card number
	public String name="Livy ";//user name
	public double accountBalance =100.6;//Account balance
	
	//Display phone card information
	public void messageCard(){
		System.out.println("Card number:"+num);
		System.out.println("The user name is:"+name);
		System.out.println("The account balance is:"+accountBalance);
	}
}
//Test class
public class TestMobileCard2 {
	public static void main(String[] args) {
		MobileCard2 card=new MobileCard2();
		card.messageCard();
	}
}

(Advanced) topic 3: application of method calls

Training points:
Class creation
Use of objects

Requirements Description:
Create Internet package class
Define the method of displaying package information and Internet service

Implementation idea:
1. Define Internet package class
2. Define member variables and member methods
3. Create and use objects

 

 

 

 

//class
package object;
import java.util.Scanner;

public class Package3{
	int taoCan=2048;//net flow
	double yueZiFei=88.0;//Monthly tariff
	int yiYong ;//Used traffic
	int shengYu ;//Residual flow
	
	public void getZHX(){
		System.out.println("China Travel Package:");
		System.out.println("Internet traffic is"+taoCan+"MB/Month, the monthly rate is"+yueZiFei+"element/Month.");
		System.out.print("Used traffic in phone card:");
		Scanner sc=new Scanner(System.in);
		yiYong=sc.nextInt();
		shengYu=2048-yiYong;
		if(shengYu>=0){
			System.out.println("Please note that you have"+shengYu+"Megaflow can be used.");
		}else{
			shengYu=-shengYu;
			System.out.println("Please note that your traffic has exceeded"+shengYu+"Megabytes.");
		}
	}
}

//Test class
package Test;

import object.Package3;

public class TestPackage {
	public static void main(String[] args) {
		Package3 zhx=new Package3();
		zhx.getZHX();
	}
}

(elevation) topic 4: comprehensive application 1

1. Case Title Description:
Define a student type
1) There are several variables: name, age, class, java score, culture lecture hall score, C language score
2) There is a way to output your own information
3) There is a way to output the total score
Test class: create object and call method
2. Scoring criteria:
(1) Correctly create student classes (10 points)
(2) Write variables correctly (10 points)
(3) Correctly write the method of displaying information (10 points)
(4) Method of correctly compiling the total score (10 points)
(5) Correctly calling method output results (10 points)
(6) Add notes (5 points)

//class
package object;

public class Student {
	public String name="Chun Chun";//full name
	public int age=18;//Age
	public String cls="Custom class";//Class
	public int javaGrade=100;//java score
	public int wenHuaGrade=99;//Achievements of culture lecture hall
	public int cGrade=100;//C language achievement
	//Output your own information
	public void myMessage(){
		System.out.println("Name:"+name);
		System.out.println("Age:"+age);
		System.out.println("Class:"+cls);
		System.out.println("java The results are:"+javaGrade);
		System.out.println("The achievements of the cultural lecture hall are:"+wenHuaGrade);
		System.out.println("C Language achievement:"+cGrade);
	}
	//Output total score
		public void sumGrade(){
			int sum=javaGrade+wenHuaGrade+cGrade;
			System.out.println("The total score is:"+sum);
		}
}

//Test class
package Test;

import object.Student;

public class TestStudent {

	public static void main(String[] args) {
		Student stu=new Student();
		stu.myMessage();
		stu.sumGrade();
	}

}

(elevation) topic 5: comprehensive application 2

Create SuperMarket (SuperMarket class)
1). Variables: supermarket name, address, telephone.
2). Method: Wumart supermarket welcomes you.
Information output method: output the variables of the supermarket.
Discount method: output "85% off price" information.
1. Scoring criteria:
(1) Correctly define classes, variables and methods (10 points)
(2) Naming standard (10 points)
(3) Create test class and call method (10 points)

//class
package object;

public class SuperMarket {
	public String name="Wumart supermarket";
	public String add="Changchun City, Jilin Province";
	public String tel="18888888888";
	public void sy(){
		System.out.println(name);
		System.out.println(add);
		System.out.println(tel);
	}
	public void daZhe(String shangPin,double price){
		double newPrice=price*0.85;
		System.out.println(shangPin+"The original price is:"+price+"Yuan, after 15% discount:"+newPrice+"element");
	}
}
//Test class
package Test;

import object.SuperMarket;

public class TestSuperMarket {
	public static void main(String[] args) {
		SuperMarket xinXi=new SuperMarket();
		xinXi.sy();
		xinXi.daZhe("Apple",10);
	}
}

(elevation) topic 6: comprehensive application 3

Demand: refit the car into a black car with three wheels.
Define the following two classes:
1: Cars.
2: Garage.
The automotive category has the following variables and methods:
String name (car name)
String color (car color)
int numTyre (number of car wheels)
run() method: output the name, color and tire number of the car, and add the sentence "running".
The garage has the following requirements:
String name (depot name)
String addr (depot address)
public Car repairCar(Car c) repair method (modify the number of wheels to 3)
Create a test class and call the repair method.
2. Scoring criteria:
Create classes correctly (10 points)
Correctly define automobile category (10 points)
Correctly define the repair shop category (10 points)
Correctly write the repair method (10 points)
Correctly call the vehicle repair method and output the effect (10 points)
Add notes (5 points)

//class
package object;

public class car {
	String name="Maserati";//(vehicle name)
	String color="gules";//(car color)
	int numTyre=4;//(number of car wheels)
	//run() method: output the name, color and tire number of the car, and add the sentence "running".
	public void run(){
		System.out.print("Name of vehicle"+name+"\t");
		System.out.print("Car color"+color+"\t");
		System.out.print("Number of tires"+numTyre+"\t");
		System.out.print("Run\n");
	}
}

//Test class
package Test;

import object.car;
import object.carRepair;

public class TestCar {

	public static void main(String[] args) {
		car c=new car();
		carRepair xiu=new carRepair();
		System.out.println("Before repair:");
		c.run();
		System.out.println("After repair:");
		xiu.RepairCar(c);
		c.run();
	}

}

(elevation) topic 7: comprehensive application 4

I Case Title Description: complete the case of women feeding dogs.
Define a Lady class:
1. Variable: name
2. Behavior: feed (dog d). The output of the feed method is that dogs eat bones.
Define Dog: Customize variables and methods.
Define test class: call the feed method of the class.
II Scoring criteria:
(1) Define women's class, and the variable behavior is correct (10 points)
(2) Define dogs and transfer dog objects into the feeding method (10 points)
(3) Define test class and call feeding method (10 points)

//Lady class
package object;

public class Lady {
	String name;
	public void feed(Dog d){
		System.out.println(d.name+"Eat bones");
	}
}

//Dog class
package object;

public class Dog {
	String name="dog";
}

//Test class
package Test;

import object.Dog;
import object.Lady;

public class TestDog {

	public static void main(String[] args) {
		Lady sb=new Lady();
		Dog d=new Dog();
		sb.feed(d);
	}

}

Topics: Java Eclipse