(Java Object Oriented) Chapter 1 Classes and Objects

1. Analog computer
package com.bdqn.demo;

import java.util.Scanner;

public class Calculator {
	double num1;
	double num2;
	int choose;//Choose the calculation method
	double he;//Calculate and
	 
    public void calc() {
    	switch(choose) {
    	case 1://calculate numbers
    		he=num1+num2;
    		break;
    	case 2:
    		he=num1-num2;
    		break;
    	case 3:
    		he=num1*num2;
    		break;
    	case 4:
    		he=(num1/num2);
    		break;
    	}
    	System.out.println(he);
    }
    public static void main(String[] args) {
		Calculator co=new Calculator();//Create object
		Scanner input=new Scanner(System.in);
		System.out.println("Please enter the first number: ");
		co.num1=input.nextInt();
		System.out.println("Please enter the second number: ");
		co.num2=input.nextInt();
		System.out.println("Please select the calculation method: (1, addition 2, subtraction 3, multiplication 4, division)");
		co.choose=input.nextInt();
		co.calc();//Call the calculation method
	}
}
2. Clock
package com.bdqn.demo;

public class Clock {
	int year;//year
	int yue; // month
	int day;//day
	int spot; // point
	int divide;//divide
	int second;//second
	
	public void CurrentTime() {//Print method
		System.out.println("Current time: "+year+"year"+yue+"month"+day+"day"+spot+"point"+divide+"minute"+second+"second");
	}
	 public static void main(String[] args) {
			Clock clo=new Clock();//Create object
			clo.year=2015;
			clo.yue=5;
			clo.day=12;
			clo.spot=10;
			clo.divide=11;
			clo.second=30;
			clo.CurrentTime();//Call method
		}
}
3. Develop the game
package com.bdqn.demo;

public class Games {
	String name;//Name
	int life;//life value, attack power
	String type;//category
	public void hero() {//method
		System.out.println("I am a hero, my basic information is as follows: ");
		System.out.println("Name: "+name+", HP: "+life);
	}
	public void arms() {
		System.out.println("I am a weapon, my basic information is as follows: ");
		System.out.println("Weapon name: "+name+", Attack power: "+life);
	}
    public void monster() {
    	System.out.println("I am a monster, my basic information is as follows: ");
    	System.out.println("Name: "+name+", HP: "+life+", Type: "+type);
    }
    public static void main(String[] args) {
		Games game=new Games();//Create object
		game.name="Li Xiaoxia";//Attribute assignment
		game.life=300;
		game.hero();//Call the method
		game.name="Death Sickle";
		game.life=12;
		game.arms();
		game.name="小龟";
		game.life=300;
		game.type="diving class";
		game.monster();
	}
}
4. Teacher class
package com.bdqn.demo;

public class Teacher {
	
	String name;//Name
	
	String major;//Professional
	
	int age;//teaching age
	
	
	public void lesson() {//method
		System.out.println(name);
		System.out.println("Professional direction: "+major);
		System.out.println("Teach the course: write programs in java");
		System.out.println("教龄:"+age);
	}

	public static void main(String[] args) {
		Teacher teacher=new Teacher();
		teacher.name="Teacher Wang";//Assignment
		
		teacher.major="computer";
		
		teacher.age=5;
		
		teacher.lesson();//Call the method
	}
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325957224&siteId=291194637