30天学会JAVA—练习题(2021韩顺平)——Day7

1. 创建程序。

定义两个类:Account 和AccountTest类,体会类的封装性。

  1. Account 类要求具有属性:姓名(长度为2/3/4位),余额(必须>20),密码(必须为6位),如果不满足,则给出提示信息,并给默认值。
  2. 通过setXxx的方法给Account 的属性赋值。
  3. 在AccountTest中测试。
public class Account {
   
	//属性
	private String name;
	private double remain;
	private String password;
	
	//封装
	public void setName(String name) {
   
		if(name.length() < 2 || name.length() >4){
   
			System.out.println("输入姓名错误,请重新输入!");
			this.name = "无名";
		}else{
   
			this.name = name;
		}

	}
	
	public String getName() {
   
		return name;
	}
	
	public void setRemain(double remain) {
   
		if(remain <= 20){
   
			System.out.println("余额错误,请重新输入!");
		}else{
   
			this.remain = remain;
		}
		
	}
	
	public double getRemain() {
   
		return remain;
	}
	
	public void setPassword(String password) {
   
		if(password.length() != 6){
   
			System.out.println("密码输入错误,请重新输入!");
			this.password = "000000";
		}else{
   
			this.password = password;
		}
		
	}	
	
	public String getPassword() {
   
		return password;
	}
	

	public void showInfo(){
   
		System.out.println("姓名:" + name + " 余额:" + remain + " 密码:" + password);
	}
}
public class AccountTest {
   
	public static void main(String[] args) {
   
		Account a = new Account();
		a.setName("abcd");
		a.setRemain(12.0);
		a.setPassword("1234");
		a.showInfo();
	}
}

2. 编写程序。

public class Computer {
   
	String cpu = "123";
	String memory = "32G";
	String disk = "16G";
	
	public void getDetails(){
   
		System.out.println("CPU:" + cpu + "内存:" + memory + "硬盘:" + disk);
	}
	public static void main(String[] args) {
   
		PC p = new PC();
		NotePad n = new NotePad();
		p.brand = "lenvo";
		n.color = "black";
		p.getDetails();
		System.out.println("品牌:" + p.brand);
		n.getDetails();
		System.out.println("演示:" + n.color);
	}
}

class PC extends Computer{
   
	String brand;
}

class NotePad extends Computer{
   
	String color;
}

3. 编写程序。

public class Person {
   
	private String name;
	private int age;
	
	public Person(String name, int age){
   
		this.name = name;
		this.age = age;
	}
		
	
	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 void say(){
   
		System.out.println("我的名字是" + name + ",年龄为" + age); 
	}
	
	public static void main(String[] args) {
   
		Person p = new Person("张三", 24);
		p.say();
		Student s = new Student("李四", 10, "123", 60);
		s.say();
	}
}

class Student extends Person{
   
	private String id;
	private double score;
	
	public Student(String name, int age, String id, double score) {
   
		super(name, age);
		this.id = id;
		this.score = score;

	}

	public String getId() {
   
		return id;
	}

	public void setId(String id) {
   
		this.id = id;
	}

	public double getScore() {
   
		return score;
	}

	public void setScore(double score) {
   
		this.score = score;
	}
	
	public void say(){
   
// System.out.println("我的名字是" + getName() + ",年龄为" + getAge()
// +",我的id为" + id + ",分数为" + score);
		super.say();
		System.out.println(",我的id为" + id + ",分数为" + score);
	}
}

4. 重写equals方法。

判断两个Person对象的内容是否相等,如果两个Person对象的各个属性值都一样,则返回true,反之false。

public class Test03 {
   
	public static void main(String[] args) {
   
		Person person1 = new Person("ada", 12, '女');
		Person person2 = new Person("ada", 12, '女');
		
		System.out.println(person1.equals(person2));//false
	}
}

class Person {
   
	private String name;
	private int age;
	private char gender;
	
	@Override
	public boolean equals(Object obj) {
   
		//判断:如果比较的两个对象是同一个对象.则直接返回true
		if(this == obj){
   
			return true;
		}
		
		//类型判断
		if( obj instanceof Person){
    //是Person,才比较
			//进行 向下转型 (因为需要得到obj的各个属性)
			Person p = (Person) obj;
			return this.name.equals(p.name) && this.age== p.age && this.gender == p.gender;
		}
		//如果不是Person,则直接返回false
		return false;

	}
	
	
	public Person(String name, int age, char gender) {
   
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	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 char getGender() {
   
		return gender;
	}

	public void setGender(char gender) {
   
		this.gender = gender;
	}		
}
全部评论

相关推荐

10-23 16:33
门头沟学院 Java
本人某中9本科,成绩中等,目前没科研没实习,目前后端学到了javaWeb,开始没定好方向,在学国外课程,走工程路线起步有点晚了,到这个时间点了还在学JavaWeb,顿感迷茫,不知道是坚持走下去还是寒假去准备考研。考研这个路弄得我还是心痒痒的,因为从众考研的人也不在少数,所以会有这方面的心理安慰吧,就是“不行我可以去考研啊”,而且意味着三年的缓冲,为了复试还有积攒经验美化简历,其实现在也可以去申入实验室打杂;就业可能意味着多些工作经验,工程岗应该到后面还是经验大于学历?还是有点迷茫了,求助好心人有无路线启发
千千倩倩:同27给点建议,现在这个时间点可以快速看完外卖和点评,不用跟着敲,但一定要在看的时候总结每个部分的整个业务流程,对其中的实现有一个大概的印象。然后直接开始看八股,刷算法。八股和算法最好还是在项目学习中穿插着看。如果计算机基础,算法这些基础好,加上每天刻苦学习,两周可以达到勉强能面试的水平,到时候就直接海投中小厂,在约面和面试的过程中不断巩固知识。没找到实习也没关系,就当积累经验。再沉淀一波直接明年三月开始投暑期,毕竟是9本,总是有面试机会的,只要你这三个月不懈怠,面试发挥得一定不错,只要拿到一个中,大厂暑期实习,秋招就有竞争力了。总得而言,现在还有机会,但是时间非常紧张,需要你结合自己情况考虑,共勉
你会选择考研还是直接就业
点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务