封装 晚上习题

一、练习题目

编写程序描述狗

二、问题描述

使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、限定心情只能有“心情好”和“心情不好”两种情况,如果无效输入进行提示,默认设置“心情好”。

3、设置构造函数实现对属性赋值

4、叫和跑的方法,需要根据心情好坏,描述不同的行为方式。

5、编写测试类,测试狗类的对象及相关方法(测试数据信息自定义)

package night.practice;

public class Dog {
	
		private String name;
		private String type;
		private int age;
		private String mood;
		
		public Dog(String name,String type,int age,String mood){
			this.name=name;
			this.age=age;
			this.type=type;
			this.mood=mood;
		}
		
		public  void run1(){
			System.out.println("心情好,跑得快");
		}
		public  void run2(){
			System.out.println("心情不好,跑得慢");
		}
		public void bark1(){
			System.out.println("心情好,叫声大");
		}
		public void bark2(){
			System.out.println("心情不好,叫声小");
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public String getMood() {
			return mood;
		}
		public void setMood(String mood) {
			this.mood = mood;
		}
}

//   测试类

package night.practice;

import java.util.Scanner;

public class TestDog {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Dog dog=new Dog("旺财","中华田园犬",2,null);
		System.out.println("请输入狗狗的心情(心情好,或心情不好):");
		Scanner sc=new Scanner(System.in);
		String mood1=sc.next();
		dog.setMood(mood1);
		if(mood1.equals("心情好")){
			dog.run1();
			dog.bark1();
		}else if(mood1.equals("心情不好")){
			dog.run2();
			dog.bark2();
		}else{
			System.out.println("狗狗心情输入输入错误,默认心情好");
			dog.run1();
			dog.bark1();
		}
	
	}

}

一、练习题目

编写程序描述IT从业者

二、问题描述

以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限;方法包括:工作

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、限定IT从业人员必须年满15岁,无效信息需提示,并设置默认年龄为15。

3、工作方法通过输入参数,接收工作单位和职务,输出个人工作信息

4、编写测试类,测试IT从业者类的对象及相关方法(测试数据信息自定义)

package night.practice;

public class Worker {
	private String name;
	private int age;
	private String direction;
	private int year;
	private String workUnit;
	private String post;
	
	 public Worker() {

	 }

	public  Worker(String name,int age,String direction,int year,String workUnit,String post){
		 this.name=name;
		 this.age=age;
		 this.direction=direction;
		 this.year=year;
		 this.workUnit=workUnit;
		 this.post=post;
		
	 }
	 
	public void work(){
		System.out.println("姓名:"+this.name);
		System.out.println("年龄:"+this.age);
		System.out.println("工作方向:"+this.direction);
		System.out.println("工作年限:"+this.year);
		System.out.println("工作单位:"+this.workUnit);
		System.out.println("职务:"+this.post);
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String getWorkUnit() {
		return workUnit;
	}

	public void setWorkUnit(String workUnit) {
		this.workUnit = workUnit;
	}

	public String getPost() {
		return post;
	}

	public void setPost(String post) {
		this.post = post;
	}

	
}

//    测试类

package night.practice;

import java.util.Scanner;

public class TestWorker {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Worker worker=new Worker("张三",0,"java开发",3,"","");
		
		System.out.println("请输入年龄:");
		Scanner sc=new Scanner(System.in);
		int age1=sc.nextInt();
		
		if(age1<15){
			System.out.println("年龄未满15岁,改成默认15岁");
			age1=15;
		}	
		worker.setAge(age1);
		
		System.out.println("请输入工作单位:");
		Scanner sc1=new Scanner(System.in);
		String workUnit1=sc1.nextLine();
		worker.setWorkUnit(workUnit1);
		
		System.out.println("请输入职务:");
		Scanner sc2=new Scanner(System.in);
		String post1=sc2.nextLine();
		worker.setPost(post1);
		
		worker.work();
		}
	  
	
	}



一、练习题目

编写程序描述卡车信息

二、问题描述

某公司要开发“X出租公司车辆管理系统”,请用面向对象的思想设计卡车类。

设定:

属性:车牌号、车型、颜色、日租金、载重量

方法:租赁

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、租赁方法通过输入参数,接收租车人姓名和租赁时间,描述租赁状态,要求判断租赁时间的有效性。

3、设计构造函数实现属性赋值

4、编写测试类,测试卡车类的对象及相关方法(测试数据信息自定义)

package night.practice;

public class Car{
	private String carNumber;
	private String carXing;
	private String color;
	private int dayPrice;
	private int weight;
	private String userName;
	private int time;
	
	public Car(String carNumber,String carXing,String color,int dayPrice,int weight){
		this.carNumber=carNumber;
		this.carXing=carXing;
		this.color=color;
		this.dayPrice=dayPrice;
		this.weight=weight;
		
	}
	public Car(){
		
	}
	public void zudai(String userName,int time){
		System.out.println("车牌号是:"+carNumber);
		System.out.println("车型是:"+carXing);
		System.out.println("车颜色是:"+color);
		System.out.println("日租金是:"+dayPrice);
		System.out.println("载重量是:"+weight);
		System.out.println("租车人姓名是:"+userName);
		System.out.println("租车人时间是:"+time);
		if(time<3){
			System.out.println("还在租车时间内!");
		}else{
			System.out.println("超出了租车时间!");
		}
	}

	public String getCarNumber() {
		return carNumber;
	}

	public void setCarNumber(String carNumber) {
		this.carNumber = carNumber;
	}

	public String getCarXing() {
		return carXing;
	}

	public void setCarXing(String carXing) {
		this.carXing = carXing;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getDayPrice() {
		return dayPrice;
	}

	public void setDayPrice(int dayPrice) {
		this.dayPrice = dayPrice;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}
	
		
}


//   测试类

package night.practice;

import java.util.Scanner;

public class TestCar {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Car car=new Car("10001","SUV","白色",100,5);
			System.out.println("请输入租车时间:");
			Scanner sc=new Scanner(System.in);
			int time1=sc.nextInt();
			car.setTime(time1);
		    car.zudai("张三",time1);
//		    Car car1=new Car();
//        	car.zudai();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值