Java第二次作业
1.编写“人”类及其测试类。属性:姓名、性别、年龄、身份证号码。
1 package sss; 2 import java.util.Scanner; 3 public class Person 4 { 5 String name; 6 char sex; 7 int age; 8 String idcard; 9 public void println() 10 { 11 System.out.print("姓名是:"+name ); 12 System.out.print("性别是:"+sex); 13 System.out.print("年龄是:"+age); 14 System.out.println("身份证号码是:"+idcard); 15 } 16 public Person(String name,char sex,int age,String IdCard) 17 { 18 this.name=name; 19 this.sex=sex; 20 this.age=age; 21 this.idcard=idcard; 22 23 } 24 } 25 public class TestPerson 26 { 27 public static void main(String[] args) 28 { 29 Scanner in= new Scanner(System.in); 30 String name; 31 char sex; 32 int age; 33 String idcard; 34 System.out.println("请输入姓名,性别,年龄,身份证号码:"); 35 name=in.next(); 36 sex=in.next().charAt(0); 37 age=in.nextInt(); 38 IdCard=in.next(); 39 Person object=new Person(name,sex,age,idcard); 40 object.println(); 41 } 42 }
2.编写“手机”类及其测试类。属性:手机品牌、手机型号。
1 package ccc; 2 import java.util.Scanner; 3 public class Phone 4 { 5 String Phonebrand; /*手机品牌*/ 6 String Phonemodel; /*手机型号*/ 7 public void println() 8 { 9 System.out.println("该手机的品牌是:"+Phonebrand); 10 System.out.println("手机型号是:"+Phonemodel); 11 } 12 13 public Phone(String Phonebrand,String Phonemodel) 14 { 15 this.Phonebrand=Phonebrand; 16 this.Phonemodel=Phonemodel; 17 } 18 } 19 public class TestPhone { 20 21 public static void main(String[] args) 22 { 23 Scanner in=new Scanner(System.in); 24 String Phonebrand,Phonemodel; 25 System.out.println("请输入手机品牌和型号:"); 26 Phonebrand=in.next(); 27 Phonemodel=in.next(); 28 Phone object= new Phone(Phonebrand,Phonemodel); 29 object.println(); 30 } 31 32 }
3.编写“书籍”类及其测试类。属性:书名、书号、主编、出版社、出版时间、页数、价格。
1 package ccc; 2 public class Book 3 { 4 private String bookname; 5 private String booknum; 6 private String bookauthor; 7 private String publishhouse; 8 private String booktime; 9 private int bookpage; 10 private double bookprice; 11 12 public Book( 13 String bookname, String booknum, 14 String bookauthor, String publishhouse, 15 String booktime, int bookpage,double bookprice 16 ) 17 { 18 super(); 19 this.bookname = bookname; 20 this.booknum = booknum; 21 this.bookauthor = bookauthor; 22 this.publishhouse = publishhouse; 23 this.booktime = booktime; 24 this.bookpage = bookpage; 25 this.bookprice = bookprice; 26 } 27 28 public void OutputInformation() 29 { 30 System.out.println("书名:"+bookname 书号:"+booknum); 31 System.out.println("主编:"+bookauthor 出版社:"+publishhouse); 32 System.out.println("出版时间:"+booktime 页数:"+bookpage); 33 System.out.println("价格:"+bookprice); 34 } 35 } 36 public class TestBook 37 { 38 public static void main(String args[]) 39 { 40 Book b1=new Book("视听说“, 41 "12354","朱晓央", 42 "上海外语教育出版社","2015","236", 43 ,45.90); 44 Book b2=new Book(”读写“, 45 "5456","郑树棠", 46 "外研社,"2017","106", 47 45.00 ,); 48 b1.OutputInformation(); 49 System.out.println(); 50 b2.OutputInformation(); 51 } 52 53 }
4.圆柱体类
1 package zzz; 2 3 public class Circle 4 { 5 final static double PI=3.1415; 6 private double radius; 7 public Circle() 8 { 9 radius=0; 10 } 11 public Circle(double r) 12 { 13 radius=r; 14 } 15 public double getRadius() 16 { 17 return radius; 18 } 19 public double getPerimeter() //计算圆的周长 20 { 21 return radius*2*PI; 22 } 23 public double getArea() //计算圆的面积 24 { 25 return radius*radius*PI; 26 } 27 28 } 29 class Cylinder extends Circle 30 { 31 double height; 32 public Cylinder(double r,double h) 33 { 34 super(r); 35 height=h; 36 } 37 public double getHeight() { 38 return height; 39 } 40 public double getCylinderArea() //计算圆柱体表面积 41 { 42 return super.getPerimeter()*height+getArea()*2; 43 } 44 double getVol() //计算圆柱体的体积 45 { 46 return super.getArea()*height; 47 } 48 public void dispVol() 49 { 50 System.out.println("半径为:"+getRadius()); 51 System.out.println("高为:"+getHeight()); 52 System.out.println("底面积为:"+getArea()); 53 System.out.println("表面积为:"+getPerimeter()); 54 System.out.println("体积为:"+getCylinderArea()); 55 } 56 57 } 58 public class TestCylinder 59 { 60 public static void main(String[] args) 61 { 62 Cylinder c1=new Cylinder(10,20); 63 Cylinder c2=new Cylinder(10,40); 64 System.out.println("圆柱体c1的信息为:"); 65 c1.dispVol(); 66 System.out.println(); 67 System.out.println("圆柱体c2的信息为:"); 68 c2.dispVol(); 69 70 } 71 72 }