课后练习题1java
定义一个圆形Circle类。程序员
属性:学习
r:半径测试
构造方法:this
无参构造方法编码
满参构造方法spa
成员方法:code
get/set方法对象
showArea方法:打印圆形面积ci
showPerimeter方法:打印圆形周长
定义测试类,建立Circle对象,并测试。
public class Circle { double r; public Circle(){
} public Circle(double r){ this.r=r; }
public double getR() { return r; }
public void setR(double r) { this.r = r; }
//showArea方法:打印圆形面积 public void showArea(){ System.out.println("圆的半径"+r); System.out.println("圆的面积:"+(3.14*r*r)); } //showPerimeter方法:打印圆形周长 public void showPerimeter(){ System.out.println("圆的半径"+r); System.out.println("圆的周长"+(2*3.14*r)); } public static void main(String[] args) { //建立对象 Circle tt=new Circle(4); tt.showArea(); tt.showPerimeter(); } } |
课后练习题2
定义一个日期MyDate类。
属性:
year:年
month:月
day:日
构造方法:
满参构造方法
成员方法:
get/set方法
showDate方法:打印日期。
isBi方法:判断当前日期是不是闰年
定义测试类,建立MyDate对象,并测试。
public class MyDate { int year; int month; int day; public MyDate(int year,int month,int day){ this.year=year; this.month=month; this.day=day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } //showDate方法:打印日期。 public void showDate(){ System.out.println(year+"年"+month+"月"+day+"日"); } //判断当前日期是否为闰年 public void isBi(){ if(year%4==0&year%400==0||year%100!=0){ System.out.println(year+"是闰年"); }else{ System.out.println(year+"不是闰年"); } } public static void main(String[] args) { //建立当前类的对象 MyDate date=new MyDate(2008,12,8); date.showDate(); date.isBi(); } } |
课后练习题三:
定义一个扑克Card类。
属性:
花色
点数
构造方法:
满参构造方法
成员方法:
showCard方法:打印牌面信息
定义测试类,建立Card对象,调用showCard方法。
代码实现,效果如图所示:
public class Card { private String hua; private String check; public Card(String hua,String check){ this.hua=hua; this.check=check; }
public String getHua() { return hua; }
public void setHua(String hua) { this.hua = hua; }
public String getCheck() { return check; }
public void setCheck(String check) { this.check = check; } //定义showCard方法,打印牌面的信息 public void showCard(){ System.out.println("牌面的信息:"+hua+check); }
public static void main(String[] args) { //建立当前类的对象 Card card=new Card("黑桃","A"); card.showCard(); } } |
课后练习题三:
定义两个类,经理类Manager,程序员类Coder
Coder类:
属性:姓名,工号,薪资
构造方法:无参构造方法,满参构造方法
成员方法:
get/set方法
intro方法:打印姓名,工号信息
showSalary方法:打印薪资信息
work方法:打印工做信息 Manager类: 属性:姓名,工号,薪资 经理的薪资有两部分组成:基本工资+奖金 构造方法:无参构造方法,满参构造方法 成员方法: get/set方法 intro方法:打印姓名,工号信息 showSalary方法:打印薪资信息 work方法:打印工做信息 定义测试类,建立Manager对象,建立Coder对象,并测试。
public class Manager { String name;//姓名 String hao; //工号 Double money;//基本工资, Double getMoneys;//奖金。 public Manager(){
} public Manager(String name,String hao,Double money,Double getMoneys){ this.name=name; this.hao=hao; this.money=money; this.getMoneys=getMoneys; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getHao() { return hao; }
public void setHao(String hao) { this.hao = hao; }
public Double getMoney() { return money; }
public void setMoney(Double money) { this.money = money; }
public Double getGetMoneys() { return getMoneys; }
public void setGetMoneys(Double getMoneys) { this.getMoneys = getMoneys; } //intro方法:打印姓名,工号信息 public void intro(){ System.out.println("姓名:"+name+" 工号"+hao); } //showSalary方法:打印薪资信息 public void showSalary(){ System.out.println("基本工资"+money+" 奖金"+getMoneys); } //work方法:打印工做信息*/ public void work(){ System.out.println("正在努力的作着管理的工做,分派任任务,检查员工提交上来的代码"); } } public class Coder { String name; String gonghao; String money; public Coder(){
} public Coder(String name,String gonghao,String money){ this.name=name; this.gonghao=gonghao; this.money=money; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getGonghao() { return gonghao; }
public void setGonghao(String gonghao) { this.gonghao = gonghao; }
public String getMoney() { return money; }
public void setMoney(String money) { this.money = money; } //此方法打印姓名,工号信息。 public void intro(){ System.out.println("姓名:"+name+"工号"+gonghao); } //此方法打印showSalary 薪资信息 public void showSalary(){ System.out.println("Corder程序猿的基本薪资"+money+" 奖金无"); } //此方法打印Corder 工做的信息 public void work(){ System.out.println("正在努力写代码"); } } 测试类 //定义经理测试类和程序猿的测试类 public class TestMangerCoder { public static void main(String[]args){ //建立经理类 Manager manager=new Manager("James","9527",15000.0,3000.0); manager.intro(); manager.showSalary(); manager.work(); System.out.println("===================="); //建立程序猿类 Coder coder=new Coder("Kobe","0025","10000"); coder.intro(); coder.showSalary(); coder.work(); } } |
课后练习题五:
定义两个类,经理类Manager,程序员类Coder
Coder类:
属性:姓名,工号,薪资
构造方法:无参构造方法,满参构造方法
成员方法:
get/set方法
intro方法:打印姓名,工号信息
showSalary方法:打印薪资信息
work方法:打印工做信息 Manager类: 属性:姓名,工号,薪资 经理的薪资有两部分组成:基本工资+奖金 构造方法:无参构造方法,满参构造方法 成员方法: get/set方法 intro方法:打印姓名,工号信息 showSalary方法:打印薪资信息 work方法:打印工做信息 定义测试类,建立Manager对象,建立Coder对象,并测试。
public class Coder { String name; String gonghao; String money; public Coder(){
} public Coder(String name,String gonghao,String money){ this.name=name; this.gonghao=gonghao; this.money=money; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getGonghao() { return gonghao; }
public void setGonghao(String gonghao) { this.gonghao = gonghao; }
public String getMoney() { return money; }
public void setMoney(String money) { this.money = money; } //此方法打印姓名,工号信息。 public void intro(){ System.out.println("姓名:"+name+"工号"+gonghao); } //此方法打印showSalary 薪资信息 public void showSalary(){ System.out.println("Corder程序猿的基本薪资"+money+" 奖金无"); } //此方法打印Corder 工做的信息 public void work(){ System.out.println("正在努力写代码"); } } /定义经理测试类和程序猿的测试类 public class TestMangerCoder { public static void main(String[]args){ //建立经理类 Manager manager=new Manager("James","9527",15000.0,3000.0); manager.intro(); manager.showSalary(); manager.work(); System.out.println("===================="); //建立程序猿类 Coder coder=new Coder("Kobe","0025","10000"); coder.intro(); coder.showSalary(); coder.work();
} } |
课后练习题六:
老师类Teacher 属性:姓名name,年龄age,讲课内容content 成员方法:吃饭eat方法,讲课teach方法 学生类Student 属性:姓名name,年龄age,学习内容content 行为:吃饭eat方法, 学习study方法
//定义老师类 public class Teatcher { String name; Integer age; String content; public Teatcher(String name,Integer age,String content){ this.name=name; this.age=age; this.content=content; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; } //吃饭的方法 public void eat(){ System.out.println("年龄为"+age+"岁的"+name+"正在吃饭。。。"); } //讲课的方法 public void teach(){ System.out.println("年龄为"+age+"岁的"+name+"正在亢奋的讲着"+content+"面向对象的知识"); } } //定义学生类 public class Student { String name; Integer age; String content;
public Student(String name,Integer age,String content){ this.name=name; this.age=age; this.content=content; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; } //吃饭的方法 public void eat(){ System.out.println("年龄为"+age+"岁的"+name+"同窗正在吃饭。。。。"); } //学习的方法 public void study(){ System.out.println("年龄为"+age+"岁的"+name+"同窗正在专心致志的听着"+content+"面向对象的知识");; } } //定义测试类 public class StudentTeacher { public static void main(String[] args) { //建立Teacher 的对象 Teatcher teatcher=new Teatcher("孙老师",30,"Java"); teatcher.eat(); teatcher.teach(); System.out.println("==================="); //建立学生的对象 Student student=new Student("韩成双",22,"Java"); student.eat(); student.study(); } } //运行的结果 // 年龄为30岁的孙老师正在吃饭。。。 // 年龄为30岁的孙老师正在亢奋的讲着Java面向对象的知识 // =================== // 年龄为22岁的韩成双同窗正在吃饭。。。。 // 年龄为22岁的韩成双同窗正在专心致志的听着Java面向对象的知识 |
课后练习题七:
判断两只猫的属性是否相同。
定义一个猫Cat类。 属性:年龄int类型,颜色char类型 构造方法: 无参构造方法,满参构造方法 成员方法: get/set方法 showMsg方法:打印猫的属性信息 定义测试类 main方法中,使用满参构造方法,建立Cat对象。 测试类中,定义比较方法,判断两只猫属性是否彻底同样。属性彻底相同返回true,一旦不一样则返回 false。
public class Cat { Integer age; String colour; public Cat(){
} public Cat(Integer age,String colour){ this.age=age; this.colour=colour; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getColour() { return colour; }
public void setColour(String colour) { this.colour = colour; } // showMsg方法:打印猫的属性信息 public void showMsg(){ System.out.println("Cat的年龄:"+age+" 颜色为:"+colour); }
} //测试类 public class TestCat { public static void main(String[] args) { //建立对象 Cat xx=new Cat(23,"白色"); xx.showMsg(); System.out.println("============================="); //建立对象 Cat cat=new Cat(24,"黑色"); cat.showMsg(); boolean flag=equals(xx,cat); System.out.println("属性是否相同: "+flag); } //定义比较的方法 public static Boolean equals(Cat xx,Cat cat){ if(xx.getAge().equals(cat.getAge())&&xx.getColour().equals(cat.getColour())){ return true; }else{ return false; } } } |
课后练习题八:
最贵的书。 定义一个图书Book类。 属性:图书编号,书名,ISBN编码,价格,出版日期 构造方法: 无参构造方法,满参构造方法 成员方法: get/set方法 showBook方法,输出图书信息 定义测试类,使用满参构造方法,建立三个Book对象,判断价格最贵的图书,并输出图书信息。
public class Book { Integer num;//编号 String name;//书名 Integer ISbn;//编码 Double price;//价格 String date;//日期 public Book(){
} public Book(Integer num,String name,Integer ISbn,Double price,String date){ this.num=num; this.name=name; this.ISbn=ISbn; this.price=price; this.date=date; }
public Integer getNum() { return num; }
public void setNum(Integer num) { this.num = num; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getISbn() { return ISbn; }
public void setISbn(Integer ISbn) { this.ISbn = ISbn; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
public String getDate() { return date; }
public void setDate(String date) { this.date = date; } //showBook方法,输出图书信息 public void showBook(){ System.out.println("编号:"+num+" 书名:"+name+" 编号:"+ISbn+" 价格:"+price+" 日期:"+date); } } //测试类
//图书的测试类 public class TestBook { public static void main(String[] args) { //建立对象 Book book0=new Book(3400,"javaEeb",1314,234.67,"2019-08-01"); Book book1=new Book(3401,"javaSE",1234,434.47,"2018-09-01"); Book book2=new Book(3402,"Java基础",1324,834.77,"2019-11-01");
//定义Book类型变量,保存最贵的图书对象。 Book expensive=book0; //先存储一本书对象的引用
if(book1.getPrice()>expensive.getPrice()){ expensive=book1; } if(book2.getPrice()>expensive.getPrice()){ expensive=book2; } System.out.println("最贵的书是:"); expensive.showBook(); } } //运行的结果: //最贵的书是: //编号:3402 书名:Java基础 编号:1324 价格:834.77 日期:2019-11-01
Lwj@ |