java对象与类
1.设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。
源代码:
1 package test; 2 3 public class t1 { 4 private String Name;//车主姓名 5 private float Speed;//当前车速 6 private float Degree;//当前方向盘转向角度 7 public t1(String Name){ 8 this.Name = Name; 9 } 10 public t1(String name,float speed,float degree){ 11 this(name); 12 this.Speed = speed; 13 this.Degree = degree; 14 } 15 public String getName(){//车主姓名访问 16 return Name; 17 } 18 public float getDegree(){//当前方向盘转向角度访问 19 return Degree; 20 } 21 public float getSpeed(){//当前车速访问 22 return Speed; 23 } 24 public void changeSpeed(float Speed){//提供改变当前的车速 25 this.Speed = Speed; 26 } 27 public void stop(){//提供停车 28 this.Speed = 0; 29 } 30 public static void main(String[] args){ 31 t1 car = new t1("小明:",200f,25f); 32 System.out.println("车主姓名:" +car.getName()); 33 System.out.println("当前车速:"+car.getSpeed()); 34 System.out.println("当前方向盘转向角:"+car.getDegree()); 35 car.changeSpeed(80); 36 System.out.println("在调用changeSpeed(80)后,车速变为:"+car.getSpeed());
2.定义一个类实现银行帐户的概念,包括的变量有"帐号"和"存款余额",包括的方法有"存款"、"取款"、"查询余额"和”显示帐号”。定义主类,创建帐户类的对象,并完成相应操作。
源代码:
1 package test; 2 import java.util.*; 3 public class t3 { 4 private String User;//账户 5 private double Money;//当前余额 6 private double add;//存款金额 7 private double subtract;//取款金额 8 public t3(String User){ 9 this.User = User; 10 } 11 public t3(String User,double Money){ 12 this(User); 13 this.Money = Money; 14 } 15 public String getUser(){//用户账号访问 16 return User; 17 } 18 public double getMoney(){//当前余额访问 19 return Money; 20 } 21 public void addMoney(double Money){//存款 22 this.Money+=Money; 23 } 24 public void subtractMoney(double Money){//取款 25 this.Money-= Money; 26 } 27 public static void main(String[] args){ 28 t3 bank = new t3("12345678",34537.876); 29 System.out.println("账号:" +bank.getUser()); 30 System.out.println("当前余额:"+bank.getMoney()); 31 Scanner in = new Scanner(System.in); 32 System.out.println("输入存款金额"); 33 double add = in.nextDouble(); 34 System.out.println("输入取款金额"); 35 double sub = in.nextDouble(); 36 bank.addMoney(add); 37 System.out.println("存款后,当前余额:" +bank.getMoney()); 38 bank.subtractMoney(sub); 39 System.out.println("取款后,当前余额:"+bank.getMoney()); 40 } 41 }
3.
设计一个类Shape(图形)包含求面积和周长的area()方法和perimeter()方法以及设置颜色的方法SetColor(),并利用Java多态技术设计其子类Circle(圆形)类、Rectangle(矩形)类和Triangle(三角形)类,并分别实现相应的求面积和求周长的方法。每个类都要覆盖toString方法。
海伦公式:三角形的面积等于s(s-a)(s-b)(s-c)的开方,其中s=(a+b+c)/2
1 package test; 2 public class t3 { 3 public static void main(String[] args) { 4 Circle c = new Circle(4,"white"); 5 System.out.println(c.toString()); 6 Rectangle r = new Rectangle(4, 5, "white"); 7 System.out.println(r.toString()); 8 Triangle t = new Triangle(3,4,5,"black"); 9 System.out.println(t.toString()); 10 } } 11 abstract class Shape{ 12 String color="red"; 13 public double area(){ 14 return -1; 15 } 16 public double perimeter(){ 17 return -1; 18 } 19 public String SetColor(String a){ 20 return color=a; 21 } 22 } 23 class Circle extends Shape{ 24 private double bj; 25 Circle(){ 26 bj=1; 27 } 28 Circle(double i,String col){ 29 bj=i; 30 super.color=super.SetColor(col); 31 } 32 public void setBj(double b){ 33 this.bj = b; 34 } 35 public double area(){ 36 return Math.PI * bj * bj; } 37 public double perimeter(){ 38 return Math.PI * bj * 2; 39 } 40 public String toString(){ 41 return( "圆的半径为" + this.bj+"圆的面积:"+this.area()+"圆的周长:"+this.perimeter()); 42 } } 43 class Rectangle extends Shape{ 44 double h; 45 double w; 46 Rectangle(){ 47 } 48 Rectangle(double hi,double wi,String col){ 49 h=hi; 50 w=wi; 51 super.color=super.SetColor(col); 52 } 53 public double area(){ 54 return h * w; } 55 public double perimeter(){ 56 return (w+h)* 2; 57 } 58 public String toString(){ 59 return( "矩形的长为" + this.h+"矩形的宽为"+this.w+"矩形的面积:"+this.area()+"矩形的周长:"+this.perimeter()); 60 } } 61 class Triangle extends Shape{ 62 double a; 63 double b; 64 double c; 65 Triangle(){ 66 } 67 Triangle(double aa,double bb,double cc,String col){ 68 a=aa; 69 b=bb; 70 c=cc; 71 super.color=super.SetColor(col); 72 } 73 public double area(){ 74 double s =((a+b+c)/2); 75 return Math.sqrt (s*(s-a)*(s-b)*(s-c)); 76 } 77 public double perimeter(){ 78 return a+b+c; 79 } 80 public String toString(){ 81 return( "三角形的长为" + this.a+"三角形的宽为"+this.b+"三角形的斜边"+this.c+"三角形的面积:"+this.area()+"三角形的周长:"+this.perimeter()); 82 } 83 }
本文来自博客园,作者:、晚风、,转载请注明原文链接:https://www.cnblogs.com/xgcl/p/13852567.html