zoukankan      html  css  js  c++  java
  • Java_5上课练习

    1、定义一个点类Point,包含2个成员变量x、y分
    别表示x和y坐标,2个构造器Point()和Point(int
    x0,y0),以及一个movePoint(int dx,int dy)方法实
    现点的位置移动,创建两个Point对象p1、p2,分
    别调用movePoint方法后,打印p1和p2的坐标。
    
    public class Point {
            int x;
            int y;
            Point(){
                System.out.println(x);
                System.out.println(y);
            }
            Point(int x0,int y0){
                System.out.println(x0);
                System.out.println(y0);
            }
             void movePoint(int dx,int dy){
                int x0;
                int y0;
                x0=(dx+5)*2;
                y0=(dy+5)*2;
                System.out.println("x="+x0);
                System.out.println("y="+y0);
            }
    }    
    
    public class test_Point {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
                Point p1=new Point();
                p1.x=2;
                p1.y=3;
                
                p1.movePoint(2, 3);
                Point p2=new Point();
                p2.x=10;
                p2.y=11;
                p2.movePoint(10,11);
    
        }
    
    }

    2、定义一个矩形类Rectangle:(知识点:对象的
    创建和使用)[必做题]
    • 2.1 定义三个方法:getArea()求面积、getPer()求
    周长,showAll()分别在控制台输出长、宽、面积
    、周长。
    • 2.2 有2个属性:长length、宽width
    • 2.3 通过构造方法Rectangle(int width, int length),
    分别给两个属性赋值
    • 2.4 创建一个Rectangle对象,并输出相关信息
    public class rectangle
    {
        int length;
        int width;
        int sum;
        int sum1;
        int getArea(int length,int width)
        {
            
            sum=length*width;
            return sum;
        }
        int getPer(int length,int width)
        {
            
            sum1=(length+width)*2;
            return sum1;
        }
        void showAll()
        {
            System.out.println("长:"+length+"宽:"+width+"面积:"+sum+"周长:"+sum1);
        
            
        }
    }
    
    public class rectangleDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            rectangle c=new rectangle();
            c.length=7;
            c.width=6;
            c.getArea(7,6);
            c.getPer(7,6);
            c.showAll();
        }
    
    }

    3、定义一个笔记本类,该类有颜色(char)和cpu
    型号(int)两个属性。 [必做题]
    • 3.1 无参和有参的两个构造方法;有参构造方法可
    以在创建对象的同时为每个属性赋值;
    • 3.2 输出笔记本信息的方法
    • 3.3 然后编写一个测试类,测试笔记本类的各个
    方法。
    
    public class computer {
            char color;
            int cup;
            void show(char color,int  cup){
                 color='';
                 cup=1213;
                System.out.println("颜色:"+color);
                System.out.println("型号:"+cup);
            }
            void show()
            {
                System.out.println("颜色:"+color);
                System.out.println("型号:"+cup);
            }
            
    }
    class test_computer 
    {
        public static void main(String[] args)
        {
            computer c=new computer();
            c.color='';
            c.cup=1212;
            c.show();
            
        }
    }

    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,441.746.2.2分别调用对象的sayHello()方法。
    package demo.java;
    
    public class test_person {
    
        public static void main(String[] args) {
        
                person p=new person();
                p.name="李路";
                p.sayHello();
                personCreate a=new personCreate();
                a.name="zhagsan";
                a.height=1.73;
                a.age=33;
                a.show();        
                personCreate a1=new personCreate();
                a1.name="lishi";
                a1.height=1.74;
                a1.age=44;
                a1.show();
        }
    
    }
    package demo.java;
    
    
    public class person {
            double height;
            String name;
            int age;
            void sayHello()
            {
            System.out.println("hello,my name is:"+name);
            }
    }
    package demo.java;
    
    public class personCreate {
        
            String name;
            double height;
            int age;
            void show(){
            System.out.println("姓名:"+name+"身高:"+height+"年龄:"+age);
            
            }
        
    }

  • 相关阅读:
    在Windows下Mysql如何重置root用户密码
    textrendering 详解
    修复IE6 PNG不透明问题的最佳解决方案
    机电传动控制第一周学习笔记
    WEB架构师成长之路之二大牛的法宝
    WEB架构师成长之路之一走正确的路
    WEB架构师成长之路之三架构师都要懂哪些知识
    hdu 1081 To The Max(最大子段和的升级版,二维)
    hdu 1080 (DP LCS最长公共子序列)
    添加\删除注册表项
  • 原文地址:https://www.cnblogs.com/108-com/p/12808018.html
Copyright © 2011-2022 走看看