JAVA第八次作业
1.

package xhy; public class Point { public static class Point{ int x; int y; public Point(int x0, int y0) { super(); this.x=x0; this.y=y0; } public Point() { super(); } public String movePoint(int dx, int dy) { x=dx+x; y=dy+y; return("x为"+x+"y为"+y); } public static void main(String[] args) { Point p1=new Point(1,2); System.out.println(p1.movePoint(4, 3)); Point p2=new Point(3,4); System.out.println(p2.movePoint(4, 5)); } } }
package xhy; public class Rectangle { int length; int width; public int getArea(int length, int width) { return length * width; } public int getPer(int length, int width) { return(length + width)* 2; } public void showAll(){ System.out.println("长方形的长为"+length+"宽为"+width+"周长为"+(length+width)*2+"面积为"+length*width); } public Rectangle(int length,int width) { super(); this.length=length; this.width=width; } public static void main(String[]args) { Rectangle r=new Rectangle(1,2); r.showAll(); } }
package xhy; public class Computer { char coulour; int cpu; public void show() { System.out.println("笔记本颜色是"+coulour+"色"+"型号是"+cpu); } public Computer(char coulour,int cpu) { super(); this.coulour=coulour; this.cpu=cpu; } public Computer() { super(); } } package xhy; public class com { public static void main(String[] args) { // TODO Auto-generated method stub Computer c=new Computer(); c.coulour='黑'; c.cpu=123; c.show(); Computer c1=new Computer('白',456); c1.show(); } }
package xhy; public class person { private String names; private int age; private double height; public String getNames() { return names; } public void setNames(String names) { this.names = names; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public person(String names, int age, double height) { super(); this.names = names; this.age = age; this.height = height; } public void sayHello() { System.out.println("hello,my name is"+" "+names); System.out.println("年龄为"+age+"身高为"+height); } } package xhy; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub person a1=new person("www",18,1.68); a1.sayHello(); person a2=new person("yyy",18,1.78); a2.sayHello(); } }
6.
定义一个汽车类Vehicle,要求如下:[选做题]
1.属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。
2.至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
3.为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
4.定义一个一般方法run(),用打印语句描述汽车奔跑
的功能
5.定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
package why; public class Vehicle { private String brand; private String color; private double speed; public Vehicle(String brand,String color){ this.brand=brand; this.color=color; } Vehicle(String brand, String color, double speed) { super(); this.brand = brand; this.color = color; this.speed = speed; } public void run(){ System.out.println("这个汽车的品牌为"+this.brand+"这个汽车的颜色为"+this.color+"这个汽车的速度为"+this.speed); } } package wen; public class VehicleTest { public static void main(String[] args) { Vehicle v=new Vehicle("benz","black"); v.run(); Vehicle v1=new Vehicle("benz","black",13); v1.run(); } }