第九周上机练习

1、定义一个点类Point,包含2个成员变量x、y分
别表示x和y坐标,2个构造器Point()和Point(int
x0,y0),以及一个movePoint(int dx,int dy)方法实
现点的位置移动,创建两个Point对象p1、p2,分
别调用movePoint方法后,打印p1和p2的坐标。[
必作题]

 1 package as;
 2 
 3 public class Point {
 4     private int x0;
 5     private int y0;
 6     public Point(){
 7     
 8     }
 9     public Point(int x0,int y0) {
10         this.x0=x0;
11         this.y0=y0;
12     }
13     public void point1(){
14         System.out.println("无参构造("+x0+","+y0+")");
15     }
16     public void movepoint(int dx,int dy){
17         System.out.println("坐标是:("+dx+","+dy+")");
18     }
19 }
 1 package as;
 2 
 3 public class text {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Point p1=new Point();
 7         p1.movepoint(3, 4);
 8         Point p2=new Point();
 9         p2.movepoint(5, 8);
10         Point p3=new Point(12,34);
11         p3.point1();
12     }
13 }

定义一个矩形类Rectangle:(知识点:对象的创建和使用)
1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2 有2个属性:长length、宽width
3 创建一个Rectangle对象,并输出相关信息

 1 package as;
 2 
 3 public class Rectangle {
 4      double length;
 5       double width;
 6       public void getArea(){
 7           System.out.println("面积是"+length*width);
 8       }
 9       public void getPer(){
10           System.out.println("周长是"+(length+width)*2);
11       }
12       public void showAll(){
13           System.out.println("长是"+length);
14           System.out.println("宽是"+width);
15           getArea();
16           getPer();
17       }
18 }
 1 package as;
 2 import java.util.Scanner;
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner input = new Scanner(System.in);
 7         Rectangle r = new Rectangle();        
 8         System.out.println("请输入长方形的长:");
 9         r.length = input.nextInt();
10         System.out.println("请输入长方形的宽:");
11         r.width = input.nextInt();
12         r.getArea();
13         r.getPer();
14         r.showAll();
15     }
16 }

 3、定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。

 1 package as;
 2 
 3 public class Macbook {
 4     char color;
 5     int cpu;
 6     Macbook() {
 7 
 8     }
 9     Macbook(char color, int cpu) {
10         this.color = color;
11         this.cpu = cpu;
12     }
13     void a() {
14         System.out.println("颜色:" + color + "  型号:" + cpu);
15     }
16 }
1 package as;
2 
3 public class unll {
4     public static void main(String[] args) {
5         // TODO Auto-generated method stub
6         Macbook a = new Macbook('a', 8888);
7         a.a();
8     }
9 }

 6、定义两个类,描述如下: [必做题]
• 6.1定义一个人类Person:
• 6.1.1定义一个方法sayHello(),可以向对方发出
问候语“hello,my name is XXX”
• 6.1.2有三个属性:名字、身高、年龄
• 6.1.3通过构造方法,分别给三个属性赋值
• 6.2定义一个Constructor类:
• 6.2.1创建两个对象,分别是zhangsan,33岁,
1.73;lishi,44,1.74
• 6.2.2分别调用对象的sayHello()方法。

 1 package as;
 2 
 3 public class Person {
 4     String name;// 姓名
 5     double height;// 身高
 6     double age ;// 年龄
 7     Person(String name, double height, double age) {
 8         this.name = name;
 9         this.height = height;
10         this.age = age;
11     }
12     void sayHello() {
13         System.out.println("hello,my name is  " + name+" "+height+"m"+" "+age+"岁");
14     }
15 
16 }
 1 package as;
 2 
 3 public class Constructor {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Person a = new Person("zhangsan", 1.73, 33);
 7         Person b = new Person("lishi", 1.74, 44);
 8         a.sayHello();
 9         b.sayHello();
10     }
11 }

原文地址:https://www.cnblogs.com/zxp-0101/p/12807679.html