1. The teacher asked Zhang Hao to use the object-oriented idea to write a calculator class (Calculator), which can realize the addition, subtraction, multiplication and division of two integers. If you are Zhang Hao, how are you going to make it happen? Write out your thoughts.
package com.homework.demo.test1; /* * Calculator class */ public class Calculator { int num1; //first integer int num2; //second integer double num3; //floating point number to divide; public void add() { //Addition method System.out.print("The sum is: "+(num1+num2)); } public void subtract() { //Method of subtraction System.out.print("The difference is: "+(num1-num2)); } public void multiply() { //Method of multiplying System.out.print("The product is: "+(num1*num2)); } public void divider() { //method of division num3 = (num1*1.0) / (num2*1.0); System.out.print("The quotient is: "+num3); } } package com.homework.demo.test1; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Calculator calculator = new Calculator(); //Create an object of calculator class to receive System.out.print("Please enter the first integer you want to operate on: "); calculator.num1 = input.nextInt(); System.out.print("Please enter the second integer you want to operate on: "); calculator.num2 = input.nextInt(); System.out.print("Please enter the operator you want to perform: "); String count = input.next(); switch (count) { case "+": calculator.add(); break; case "-": calculator.subtract(); break; case "*": calculator.multiply(); break; case "/": calculator.divider(); break; } } }
2. Assuming that the current time is 10:11:00 on May 12, 2015, write a CurrentTime class, set the property to change the time, and define the show() method to display the changed time.
package com.homework.demo.testFirst2; public class CurrentTIme { int year = 2015; //year int month = 5; //month int date = 12; //日 int time = 10; //time int minute =11; //minute int second = 00; //seconds public void show() { System.out.println(year + "年"+month + "月"+ date + "日" + time + "点" + minute + "分 " + second + "秒"); } } package com.homework.demo.testFirst2; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub CurrentTIme curren = new CurrentTIme(); curren.show(); } }
3. Improve the second question and change the current time to 10:11:30 on May 12, 2015. Write a Demo class, change the time set in the CurrentTime class, and print the output.
package com.homework.demo.testFirst2; public class CurrentTIme { int year = 2015; //year int month = 5; //month int date = 12; //日 int time = 10; //time int minute =11; //minute int second = 00; //seconds public void show() { System.out.println(year + "年"+month + "月"+ date + "日" + time + "点" + minute + "分 " + second + "秒"); } } package com.homework.demo.testFirst2; public class Dome { public static void main(String[] args) { // TODO Auto-generated method stub CurrentTIme curren = new CurrentTIme(); //Create object curren.second = 30; curren.show(); } }
4. Describe computers using classes.
package com.homework.demo.testFirst3; public class Computer { String cpu = "Intel Core [email protected] Quad Core"; //Processor String mainBoard = "Vulcan Series"; //主板 String disPlay = "Sharp SHP142A LQ156D1JX01B"; //Display String hardDisk = "Western Digital WDC WDS240G1G0B-00RC30(240G/SSD)"; //hard disk String internalStorage = "ADATA DDR4 2133MHz (8G)"; //Memory String disPlayCard = "Nvidia GeForce GTX 1050 Ti(4G)"; //graphics card public void show() { System.out.println("Processor:"+cpu+"\nMotherboard:"+mainBoard+"\nHard Disk:"+hardDisk+"\nMemory:"+internalStorage+"\nGraphics Card:"+disPlayCard); } } package com.homework.demo.testFirst3; import com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOM; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Computer computer = new Computer(); computer.show(); } }
5. If a company wants to develop a new game, please use object-oriented thinking to design heroes, monsters and weapons. Write test classes, create hero objects, monster objects, and weapon objects, and output their respective information. The settings are as follows.
1. Hero class.
Attributes: hero name, health
Method: output basic information
2. Monster class:
Attributes: monster name, health, type
Method: output basic information
3. Weapons:
Attributes: weapon name, attack power
Method: output basic information
package com.homework.demo.testFirst4; public class Hero { String name; // hero name int health; // hero's health public void show() { //Method for outputting hero information System.out.println("Name: " + name + ", Health: " + health); } } package com.homework.demo.testFirst4; public class Monster { String name; //Monster name int health; //Monster health String type; //monster type public void show() { //Method for outputting monster information System.out.println("Name: "+name+", Health: "+health+", Type: "+type); } } package com.homework.demo.testFirst4; public class Weapon { String name; // weapon name int atk; //weapon attack power public void show() { //Method for outputting weapon information System.out.println("Weapon name: "+name+", Attack power: "+atk); } } package com.homework.demo.testFirst4; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("I am a hero, my basic information is as follows: "); Hero hero = new Hero(); //Create an object of the hero class hero.name = "Li Xiaoxia"; hero.health = 300; hero.show(); System.out.println("I am a weapon, my basic information is as follows:"); Weapon weapon = new Weapon(); //Create weapon class object weapon.name = "Reaper Scythe"; weapon.atk = 12; weapon.show(); System.out.println("I am a monster, my basic information is as follows:"); Monster monster = new Monster();//Create a monster class object monster.name = "Little Turtle"; monster.health = 300; monster.type = "Dive type"; monster.show(); } }