Java基础Day07案例源码

//1、学生类的代码及测试
class Demo_Student {
    public static void main(String[] args) {
        Student s = new Student ("张三",26);  //创建对象
        s.work();   //打印结果
    }
}
class Student{
    private String name;        //私有 name
    private int age;        //私有age
    public Student(){}      //无参构造
    public Student(String name,int age){    //有参构造
        this.name = name;
        this.age = age;
    }
    public void setName(String name ){      //设置姓名
        this.name = name ;
    }
    public String  getName(){       //获取姓名
        return name;
    }
    public void setAge(int age){    //设置年龄
        this.age = age;
    }
    public int getAge(){        //获取年龄
        return age;
    }
    public void work(){         //work方法
        System.out.println("姓名为:" + name + ",年龄为:" + age);
    }
}

//2、手机类的代码和测试
class Demo_Employee {
    public static void main(String[] args) {
        Employee e = new Employee ("唐嫣",20161212,12000.6);  //有参构造
        e.work();   //输出语句
    }
}
class Employee{
    private String name ;
    private int id;
    private double salary;
    public Employee(){} //无参构造
    public Employee(String name,int id,double salary){      //有参构造
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    public void setName(String name){       //设置姓名
        this.name = name;
    }
    public String getName(){            //获取姓名  
        return name;
    }
    public void setId(int id){          //设置id
        this.id = id ;
    }
    public int getId(){                 //获取id
        return id;
    }       
    public void setSalary (double salary){      //设置工资
        this.salary = salary;
    }
    public double getSalary(){      //获取工资
        return salary;
    } 
    public void work(){
        System.out.println("姓名为:" + name + ",工号为:" + id + ",工资为:" + salary);        //输出信息
    }
} 

//3、长方形的代码
class Exercise_Rectangle {
    public static void main(String[] args) {
        Rectangle r = new Rectangle();
        r.setWidth (10) ;       //设置宽为10
        r.setHigh (20) ;        //设置高度20
        int zhouchang =r.getLength();
        int mianji =r.getArea();
        System.out.println("周长为:" + zhouchang + "面积为:" + mianji);   //打印结果
    }
}
class Rectangle{
    private int width;
    private int high;
    public Rectangle(){}        //无参构造  
    public Rectangle(int width,int high){       //有参构造
        this.width = width;
        this.high = high;
    }
    public  void setWidth( int width){      //设置宽度
        this.width = width;
    }
    public int getWidth(){      //获取宽度
        return width;
    }
    public void setHigh (int high){     //设置宽度
        this.high = high;
    }
    public int getHigh(){   //获取宽度
        return high;
    }
    public int getLength(){     //获取周长
        return 2*(high + width);
    }
    public int getArea(){   //获取面积
        return high * width;
    }
}

//4、员工类代码和测试
class Employee {
    public static void main(String[] args) {
        Employee1 e = new Employee1 ("唐嫣",20161212,12000.6);    //有参构造
        e.work();   //输出语句
    }
}
class Employee1{
    private String name ;
    private int id;
    private double salary;
    public Employee1(){}    //无参构造
    public Employee1(String name,int id,double salary){     //有参构造
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    public void setName(String name){       //设置姓名
        this.name = name;
    }
    public String getName(){            //获取姓名  
        return name;
    }
    public void setId(int id){          //设置id
        this.id = id ;
    }
    public int getId(){                 //获取id
        return id;
    }       
    public void setSalary (double salary){      //设置工资
        this.salary = salary;
    }
    public double getSalary(){      //获取工资
        return salary;
    } 
    public void work(){
        System.out.println("姓名为:" + name + ",工号为:" + id + ",工资为:" + salary);        //输出信息
    }
} 

//5、ArrayTool类的写
class ArrayTool{
    private ArrayTool(){}  //私有构造方法
    public static int  getMax(int[] arr){       //获取最大值
        int max = arr[0];
        for (int i = 1;i < arr.length ;i++ ){
            if (max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
    public static void print(int[] arr){            //遍历数组
        for (int i = 0;i < arr.length ; i++){
            System.out.print(arr[i]);
        }
    }
    public static void  revArray(int[] arr){        //数组反转
        for (int i = 0;i < arr.length /2;i++){    //反转次数
                int temp = arr[i];          //123456   012345    05 14 23
                arr[i] = arr[arr.length-1-i];       
                arr[arr.length-1-i] = temp;     //反转数组
        }
    }

}

//6、产生一个随机数,要求这个随机数的范围在 6 - 19 ,包含6和19
class Demo_Random {
    public static void main(String[] args) {
        System.out.println("随机出现6-19中的数字"); //random*14+6 范围在6-19
        double random1 =Math.random();  
        int a =(int)(random1*14+6);     //随机数在0-1范围,*14+6在6-19
        System.out.println(a);
    }
}

//7、猜数字小游戏
import java.util.Scanner;       //导包
class gussNum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    //创建键盘录入对象
        int num =(int)((Math.random())*100 +1);     //随机数在1-100
        System.out.println("猜数字:请输入1-100的整数!");

        while(true){
            int input = sc.nextInt();       //注意键盘录入一定要放在循环里面,一开始无限循环就在于此问题
            if (input > num){
            System.out.println("大了,继续猜!");
            }else if(input < num){
                    System.out.println("小了,继续猜!");
            }else {
                System.out.println("恭喜你,猜中了");
                break;                          //猜中退出循环
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值