Java Exercise 12.2

Java Exercise 12.2

1. Case 3-1: Define student class

Case description:
Design a class that represents students. This class has the attribute name representing the name and the attribute age representing the age. It also has the method speak() representing the speaking behavior, which is used to output the name and age of the student

package com.shangjiti.aoian;
public class Student {
    
    
	String	name;
	int age;
	public void speak(){
    
    
		System.out.println("名字是"+name+"年龄是:"+age+"岁");
	}
}
2. Case 3-2: Create an object of the class

Case description:
Based on Case 3-1, create a student object stu, assign values ​​to the name and age attributes of stu, and then call the speak() method to output the student's information.

package com.shangjiti.aoian;
public class No1 {
    
    
	public static void main(String[] args) {
    
    
		Student stu=new Student();
		stu.name="Aoain";
		stu.age=3;					 //调用对象的结构:属性,对象
		System.out.println(stu.name);//调用属性:"对象.属性"								
		System.out.println(stu.age);
		stu.speak();				//调用方法:对象.方法
	}
}
3. Case 3-3: Encapsulation of Class

Case description:
This case will use the private keyword to privatize the member variables name and age, and then define four methods to access name and age externally: getName(), setName(String n), getAge() and setAge(int a) .
Check the incoming parameters in the setAge(int a) method. If the input value is negative, print out "The set age is illegal", if it is not a negative number, set it to the value of the age attribute.
Check the incoming parameters in the setName method. If it is an empty string, it will prompt "The name cannot be empty". If it is not empty, it will be set to the value of name.
Create an instance object of the Student class in the main() method, set the name attribute and age attribute value by calling the setName(String n) and setAge(int a) methods of the object, and call the speak() method.

package com.shangjiti.aoian;
class student{
    
    
	private String name;
	private int age;
	public String getName() {
    
    
		return name;
	}
	public void setName(String stuName) {
    
    
		if(stuName=="") 
		{
    
    
			System.out.println("姓名不能为空");
		}
		
		else
		{
    
    
			name=stuName;
		}
	}
	public int getAge() {
    
    
		return age;
	}
	public void setAge(int stuAge) {
    
    
		if(stuAge<=0)
		{
    
    
			System.out.println("设置年龄不合法");
		}
		else
		{
    
    
			age=stuAge;
		}
	}
	public void speak() {
    
     
		System.out.println("姓名是:"+name+"年龄为:"+age);
	}
}
public class No3{
    
    
	public static void main(String[] args) {
    
    
		student stu=new student();
		stu.setAge(-30);
		stu.setName("");
		stu.speak();
	}
}
4. Consolidation exercises

Define a Division class (division), define two private member variables of type int divide (dividend) and divisor (divisor), the default value is 1. Define four public methods setDividend(int mDividend), getDividend(), setDivisor(int mDivisor) and getDivisor() for setting and accessing private attributes. Check the incoming parameters in the setDivisor(int mDivisor) method. If the incoming value is zero, print "Divisor cannot be zero", if it is not zero, set it to the value of the divisor attribute

package com.shangjiti.aoian;
class Divison{
    
    
	private int divident=1;
	private int divisor=1;
	double  result;
	public void setDivident(int mDivident) {
    
    
		divident=mDivident;
}
	public int getDivident() {
    
    
		return divident;
	}
	public void setDivisor(int mdivisor) {
    
    
		if(mdivisor==0) {
    
    
			System.out.println("除数不能为0");
		}
		else {
    
    
			divisor=mdivisor;
		}
	}
	public int getDivisor() {
    
    
		return divisor;
	}
}
public class No5 {
    
    
	public static void main(String[] args) {
    
    
		Divison a=new Divison();		
		a.setDivident(40);
		a.setDivisor(8);
		int divident=a.getDivident();
		int divisor=a.getDivisor();
		System.out.println(divident/divisor);
	}
}

Guess you like

Origin blog.csdn.net/m0_46653702/article/details/109430070