第二次java作业
1.编写“人”类及其测试类。属性:姓名、性别、年龄、身份证号码。
package FengZhuang; import java.util.Scanner; class Person { String name; char sex; int age; String IdCard; public void printf() { System.out.print("姓名是:"+name+" "); System.out.print("性别是:"+sex+" "); System.out.print("年龄是:"+age+" "); System.out.println("身份证号码是:"+IdCard); } public Person(String name,char sex,int age,String IdCard) { this.name=name; this.sex=sex; this.age=age; this.IdCard=IdCard; } } public class TestPerson { public static void main(String[] args) { Scanner in= new Scanner(System.in); String name; char sex; int age; String IdCard; System.out.println("请输入姓名,性别,年龄,身份证号码:"); name=in.next(); sex=in.next().charAt(0); age=in.nextInt(); IdCard=in.next(); Person object=new Person(name,sex,age,IdCard); object.printf(); } }
2.编写“手机”类及其测试类。属性:手机品牌、手机型号。
package FengZhuang; import java.util.Scanner; class Phone { String Phonebrand; /*手机品牌*/ String Phonemodel; /*手机型号*/ public void printf() { System.out.print("该手机的品牌是:"+Phonebrand+" "); System.out.println("手机型号是:"+Phonemodel); } public Phone(String Phonebrand,String Phonemodel) { this.Phonebrand=Phonebrand; this.Phonemodel=Phonemodel; } } public class TestPhone { public static void main(String[] args) { Scanner in=new Scanner(System.in); String Phonebrand,Phonemodel; System.out.println("请输入手机品牌和型号:"); Phonebrand=in.next(); Phonemodel=in.next(); Phone object= new Phone(Phonebrand,Phonemodel); object.printf(); } }
3.编写“书籍”类及其测试类。属性:书名、书号、主编、出版社、出版时间、页数、价格。
package FengZhuang; import java.util.Scanner; class Book { String BookTitle, BookNumber;/*书名,书号*/ String Compiler,Press; /*主编,出版社*/ String PublishTime; /*出版时间*/ int Page,Price; /*页数,价格*/ void printf() { System.out.print("书名是:"+BookTitle+" "+"书号是:"+BookNumber+" "); System.out.print("主编是:"+Compiler+" "+"出版社是:"+Press+" "); System.out.print("出版时间是:"+PublishTime+" "); System.out.println("页数是:"+Page+" "+"价格是:"+Price); } public Book(String BookTitle,String BookNumber,String Compiler,String Press,String PublishTime,int Page,int Price) { this.BookTitle=BookTitle; this.BookNumber=BookNumber; this.Compiler=Compiler; this.Press=Press; this.PublishTime=PublishTime; this.Page=Page; this.Price=Price; } } public class TestBook { public static void main(String[] args) { Scanner in=new Scanner(System.in); String BookTitle,BookNumber; String Compiler,Press; String PublishTime; int Page,Price; System.out.println("请输入书名、书号、主编、出版社、出版时间、页数、价格"); BookTitle=in.next(); BookNumber=in.next(); Compiler=in.next(); Press=in.next(); PublishTime=in.next(); Page=in.nextInt(); Price=in.nextInt(); Book test1=new Book(BookTitle,BookNumber,Compiler,Press,PublishTime,Page,Price); Book test2=new Book("C语言从入门到放弃","1976654","谭浩强","清华大学出版社","2010年6月",390,33); test1.printf(); test2.printf(); } }
4.编写“圆柱体”类及其测试类。属性:圆底半径、高。 方法1:计算底面积 方法2:计算体积 方法3:打印圆底半径、高、底面积和体积
package FengZhuang; class Cylinder { double high,Radius;/*高,半径*/ public Cylinder(double high,double Radius) { this.high=high; this.Radius=Radius; } public double Bottomarea() /*底面积方法*/ { double s; s=2*3.14*Radius*Radius; return s; } public double volume()/*体积方法*/ { double v; v=Bottomarea()*high; return v; } public void print() { System.out.print("圆柱的半径是:"+Radius+" "+"高是:"+high+" "); System.out.println("底面积是:"+Bottomarea()+" "+"体积是:"+volume()); } } public class TestCylinder { public static void main(String[] args) { Cylinder test1=new Cylinder(6,6); Cylinder test2=new Cylinder(9,9); test1.print(); test2.print(); } }