面向对象练习(1)

面向对象练习(1)


目录

1.建立类并输出

2.密码修改

3.景区参观

4.猜数字

5.交通工具(部分超纲)

6.计算器

7.企鹅喂食


1.建立类并输出

        建立学员类及教员类:

package dh01;

//定义学员类
public class Student{
	String name;//姓名
	int age;	//年龄
	String classNo;//班级
	String like; //爱好
	//输出方法信息
	public void show() {
		System.out.println(name+"\n年龄:"+age+"\n班级:"+classNo+"\n爱好:"+like);
	}
}
package dh01;
//建立教员类
public class Teacher {
	String name;//姓名
	String major;//专业方向
	String courses;//教授课程
	int schoolAge;//教龄
	//输出方法信息
	public void show() {
		System.out.println(name+"\n专业方向:"+major+"\n教授课程:"+courses+"\n教龄:"+schoolAge);
	}
		
}

        输出相关信息:

package dh01;

public class Test01 {

	public static void main(String[] args) {
		// 练习1:创建学员类和教员类
		Student student = new Student();//创建对象
		student.name="yjz";
		student.age = 18;
		student.classNo = "J3031";
		student.like = "唱、跳、rap、篮球";
		student.show();//调用方法
//------------------------------------------------------
		Teacher teacher = new Teacher();//创建对象
		teacher.name = "wzy";//姓名
		teacher.major = "JAVA";//专业方向
		teacher.courses = "java";//教授课程
		teacher.schoolAge = 10;//教龄
		teacher.show();//调用方法 

	}

}

2.密码修改

        定义Admin类:

package dh01;

public class Admin {
	// 属性:用户名 密码
	String userName;
	String password;
	// 方法:输出管理员信息
	public void showInfo() {
		System.out.println("管理员用户名为:"+userName+": 密码为:"+password);
	}
}

        修改密码:

package dh01;

import java.util.Scanner;

public class TestAdmin {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc =new Scanner(System.in);
		//管理员信息
		Admin admin =new Admin();
		admin.userName="admin000";
		admin.password="123456";
		admin.showInfo();
		
		//修改管理员密码
		System.out.print("请输入用户名:");
		String name = sc.next();
		System.out.print("请输入密码:");
		String pwd = sc.next();
		if(admin.userName.equals(name) && admin.password.equals(pwd)) {
			System.out.print("请输入新的密码:");
			admin.password= sc.next();
			System.out.println("修改密码成功,您的新密码为:"+admin.password);
		}else {
			System.out.println("用户名和密码不正确,没有权限修改密码。");
		}
		
		
	}

}

3.景区参观

        定义游客类:

package dh01;

//游客类
public class Visitor {
	String name;
	int age;
	
	public void showPrice() {
		if(age>0&&age<=10) {
			System.out.println(name+"的年龄为:"+age+"岁:门票免费。\n");
		}
		else if(age<18) {
			System.out.println(name+"的年龄为:"+age+"岁:门票10元。\n");
			
		}else if(age<60) {
			System.out.println(name+"的年龄为:"+age+"岁:门票20元。\n");
			
		}
		else {
			System.out.println(name+"的年龄为:"+age+"岁:门票免费。\n");
			
		}
	}
	
	
	
}

        测试:

package dh01;

import java.util.Scanner;

public class TestVisitor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		Visitor v = new Visitor();
		boolean flag = true;
		do {
			System.out.print("请输入姓名:");
			String name = sc.next();
			if (!name.equals("n")) {
				v.name = name;// 将用户键盘录入的姓名赋值给v对象的name属性
				System.out.print("请输入年龄:");
				int age = sc.nextInt();
				v.age = age;// 将用户键盘录入的年龄赋值给v对象的age属性
				v.showPrice();
			}else {
				flag =false;
			}
		} while (flag);
		System.out.println("退出程序!!!");
	}

}

4.猜数字

        定义Guess类:

package dh02;

import java.util.Scanner;

//定义guess类
public class Guess {
	Scanner sc = new Scanner(System.in);
	int v;
	int num;

	public void show() {
		while (true) {
			System.out.print("请输入测试的数字:");
			num = sc.nextInt();
			if (num > v) {
				System.out.println("大了");
			} else if (num < v) {
				System.out.println("小了");
			} else {
				System.out.println("答对了");
				break;
			}
		}
	}
}

        测试:

package dh02;

import java.util.Scanner;

public class TestGuess {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 测试
		Guess gs = new Guess();
		gs.v = 100;//v赋初值100
		gs.show();//调用方法
	}

}

5.交通工具(部分超纲)

         定义交通工具类:

package dh02;

import java.util.Scanner;

//定义Vehicle类
public class Vehicle {
	Scanner sc =new Scanner(System.in);
	private int speed;
	private int size;
	
	// 方法
	
	Vehicle(int speed,int size){
		this.speed =speed;
		this.size =size;
	}// 移动
	public void move() {
		System.out.println("移动");
	}
	// 设置速度
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	//获得速度
	public int getSpeed(){
		return this.speed;
	}
	// 加速
	public void speed(int num1) {
		
		if(num1>0) {
			//加速
			System.out.print("加速:"+(this.speed+num1));	
		}else{
			//减速
			System.out.print("减速:"+(this.speed+num1));	
		}	
	}

	public void show() {
		move();
		int up=sc.nextInt();
		speed(up);
	}

}

        测试:

package dh02;

import java.util.Scanner;

//测试
public class TestVehicle {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		Vehicle car = new Vehicle(100, 8);
		car.show();
			
	}
}

6.计算器

        定义计算类:

package dh02;
//定义计算类

import java.util.Scanner;

public class Calculate {

	double num1;
	double num2;
	String a;
	Scanner sc = new Scanner(System.in);

	Calculate(double num1,double num2,String a) {
		this.num1=num1;
		this.num2=num2;
		this.a=a;
	}
	
	public void test() {
		if(a.equals("+")) {
			System.out.println(num1+"+"+num2+"="+(num1+num2));
		}if(a.equals("-")) {
			System.out.println(num1+"-"+num2+"="+(num1-num2));
		}if(a.equals("*")) {
			System.out.println(num1+"*"+num2+"="+(num1*num2));
		}if(a.equals("/")) {
			System.out.println(num1+"/"+num2+"="+(num1/num2));
		}
	}
	
	public void show() {
		test();
	}
	
	
	
}

        测试:

package dh02;

import java.util.Scanner;

public class TestCalculate {
	public static void main(String[] args) {
		
		Scanner sc =new Scanner(System.in);
		System.out.print("请输入第一个数字:");
		double num1 =sc.nextInt();
		System.out.print("请输入第二个数字:");
		double num2 =sc.nextInt();
		System.out.print("请输入操作符:");
		String a =sc.next();
		Calculate cal = new Calculate(num1, num2, a);
		cal.show();	
		
	}
}

7.企鹅喂食

        定义企鹅类:

package dh02;

public class Penguin {
	int health;//健康值
	String sex;//性别
	int love;//亲密度
	
	public void eat(int love) {
		this.love += love;
	}
	
	public void p1() {
		System.out.println("我是一只企鹅");
	}
	
	public void show() {
		System.out.println("亲密度为:"+this.love);
	}
	

}

        测试:

package dh02;

import java.util.Scanner;

public class TestPenguin {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		Penguin pg =new Penguin();
		pg.sex="公";
		pg.health=50;
		pg.p1();
		int l1=sc.nextInt();
		pg.eat(pg.love+l1);
		pg.show();
		//第二只企鹅
		Penguin pg2 =new Penguin();
		pg2.sex="母";
		pg2.health=50;
		pg2.p1();
		int l2=sc.nextInt();
		pg2.eat(pg2.love+l2);
		pg2.show();
	}

}

8.企鹅喂食修改

        定义企鹅类:

package dh02;
/**
 * 
 * 创建两个企鹅并实现企鹅的自我介绍,
并实现喂食,投递不同的食物增加不同的亲密度。
 *
 */
//企鹅类
public class Penguin {
	String name;//名字
	int health;//健康值
	String sex;//性别
	int love;//亲密度
	//方法
	//吃
	public void show() {
		System.out.println("我叫"+name+",是一只"+sex+"企鹅,"+"健康值为"+health+"亲密度为"+love);
	}

	
}

        测试:

package dh02;

import java.util.Scanner;
//测试
public class TestPenguin {
	Scanner sc =new Scanner(System.in);
	
	public static void main(String[] args) {
		FeedPenguin fp=new FeedPenguin();//实例化
		Penguin pg1 =new Penguin();//实例化
		pg1.name = "张三";
		pg1.health = 100;
		pg1.sex = "公";
		pg1.love = 0;
		fp.Feed(pg1);
		pg1.show();
		//第二只企鹅
		Penguin pg2 =new Penguin();//实例化
		pg2.name = "李四";
		pg2.health = 100;
		pg2.sex = "母";
		pg2.love = 0;
		fp.Feed(pg2);
		pg2.show();
	}

}

你可能感兴趣的:(面向对象,eclipse,java,学习)