【雪野实训记录】Java-T1作业 基础

版权声明:进击的葱 https://blog.csdn.net/qlwangcong518/article/details/77054095
package T1;

public class t1_1 {
//  1)设计显示各种水果的定购详情的类,详情包括:名称、数量、价格  
//  测试数据:"苹果", 10, 20 ; "芒果", 18, 56 ;  "桔子", 25, 75  
    private String name;  
    private double num;  
    private double price;  
    public t1_1(String name, double num, double price) {  
        super();  
        this.name = name;  
        this.num = num;  
        this.price = price;  
    }  
    /** 
     * @return the name 
     */  
    public String getName() {  
        return name;  
    }  
    /** 
     * @param name the name to set 
     */  
    public void setName(String name) {  
        this.name = name;  
    }  
    /** 
     * @return the num 
     */  
    public double getNum() {  
        return num;  
    }  
    /** 
     * @param num the num to set 
     */  
    public void setNum(double num) {  
        this.num = num;  
    }  
    /** 
     * @return the price 
     */  
    public double getPrice() {  
        return price;  
    }  
    /** 
     * @param price the price to set 
     */  
    public void setPrice(double price) {  
        this.price = price;  
    }  
      
    public void display() {  
        System.out.println(getName()+","+getNum()+","+getPrice());  
    }  
    public static void main(String[] args) {  
    	t1_1 t1 = new t1_1("苹果", 10, 10);  
    	t1_1 t2 = new t1_1("芒果", 18, 56);  
    	t1_1 t3 = new t1_1("橘子", 25, 75);  
        t1.display();  
        t2.display();  
        t3.display();          
    }  
}


package T1;

public class t1_2 {  
//  2)设计一个类用于得出三个不同盒子的体积。  
//  测试数据:2, 3, 4 ;1, 5, 6 ;3, 8, 2  
    private double a;  
    private double b;  
    private double c;  
    public t1_2(double a, double b, double c) {  
        super();  
        this.a = a;  
        this.b = b;  
        this.c = c;  
    }  
    /** 
     * @return the a 
     */  
    public double getA() {  
        return a;  
    }  
    /** 
     * @param a the a to set 
     */  
    public void setA(double a) {  
        this.a = a;  
    }  
    /** 
     * @return the b 
     */  
    public double getB() {  
        return b;  
    }  
    /** 
     * @param b the b to set 
     */  
    public void setB(double b) {  
        this.b = b;  
    }  
    /** 
     * @return the c 
     */  
    public double getC() {  
        return c;  
    }  
    /** 
     * @param c the c to set 
     */  
    public void setC(double c) {  
        this.c = c;  
    }  
      
    public void display() {  
        System.out.println(getA()+" "+getB()+" "+getC()+" "+getA()*getB()*getC());  
    }  
      
    public static void main(String[] args) {  
        t1_2 t1 = new t1_2(2, 3, 4); 
        t1_2 t2 = new t1_2(1, 5, 6);
        t1_2 t3 = new t1_2(3, 8, 2);
        t1.display(); 
        t2.display(); 
        t3.display();        
    }  
  
}  

package T1;

public class t1_3 {
//  3)设计一个Tools类提供重载方法println()和println(String info),  
//  让其接收不同的数据并能打印字符串,数字,布尔值和换行打印。  
    public void println(int a) {  
        System.out.println("int = "+a);  
    }  
    public void println(String info) {  
        System.out.println("string = info");  
    }  
    public static void main(String[] args) {  
    	t1_3 t = new t1_3();  
        t.println(3);  
        t.println("SkyCong王聪");  
    }  
}

package T1;

public class t1_4 {
//  4)设计用于将华氏温度转换为摄氏温度的类.  
//  提示:  
//  华氏温度转换为摄氏温度.公式  摄氏=(华氏-32)*5/9  
//  摄氏温度转换为华氏温度.公式  华氏=摄氏*9/5+32  
    private double fah;  
    private double deg;  
  
  
  
public t1_4(double fah) {  
        super();  
        this.fah = fah;  
    }  
  
public void zh_deg() {  
    deg = (fah-32)*5/9;  
    System.out.println(deg);  
}  
  
  
public static void main(String[] args) {  
    t1_4 t = new t1_4(10);  //华氏温度10度
    t.zh_deg();  
}  
  
  
  
/** 
 * @return the fah 
 */  
public double getfah() {  
    return fah;  
}  
  
  
  
/** 
 * @param fah the fah to set 
 */  
public void setfah(double fah) {  
    this.fah = fah;  
}  
  
}  


package T1;

public class t1_5 {
    //4)设计一个测试  
    //类,仅有一个main方法,然后在main中设定一个华氏温度,
	//然后调用上题定义的方法转换成摄氏温度,最后使用6中的
	//Tools类的print完成打印输出

	    public static void main(String[] args) {  
	        t1_4 t1_5 = new t1_4(1);   
	        t1_5.setfah(2);  
	        t1_5.zh_deg();  
	          
	    }  
}

package T1;

public class t1_6 {
	/** 
     * 6).写一个名叫Person的类表示人类, 
     * 数据域为姓名name、性别gender、年龄age,name是String类型、 
     * gender是boolean类型(true表示男性,false表示女性)、age是int类型; 
     * 人的行为有行走walk、吃eat。要求如下:  
     * a)实现walk和eat方法,分别打印“XXX在行走”,“XXX在吃东西”;XXX是具体人名  
     * b)提供main方法,创造两个人类的实例:Lucy,女,23岁;Jame,男,25岁  
     * c)在main方法中分别调用两个人类实例的walk和eat方法,观察打印结果 
     */  
      private String name;  
      private boolean gender;  
      private int age;  
    public t1_6(String name, boolean gender, int age) {  
        super();  
        this.name = name;  
        this.gender = gender;  
        this.age = age;  
    }  
    /** 
     * @return the name 
     */  
    public String getName() {  
        return name;  
    }  
    /** 
     * @param name the name to set 
     */  
    public void setName(String name) {  
        this.name = name;  
    }  
    /** 
     * @return the gender 
     */  
    public boolean isGender() {  
        return gender;  
    }  
    /** 
     * @param gender the gender to set 
     */  
    public void setGender(boolean gender) {  
        this.gender = gender;  
    }  
    /** 
     * @return the age 
     */  
    public int getAge() {  
        return age;  
    }  
    /** 
     * @param age the age to set 
     */  
    public void setAge(int age) {  
        this.age = age;  
    }  
        
    public void walk() {  
        System.out.println(getName()+"在行走");  
    }  
    public void eat() {  
        System.out.println(getName()+"在吃东西");  
    }  
    //提供main方法,创造两个人类的实例:Lucy,女,23岁;Jame,男,25岁  
    public static void main(String[] args) {  
        t1_6 t1_6 = new t1_6("Lucy", false, 23);  
        t1_6.walk();  
        t1_6.eat();  
        t1_6.setName("Jame");  
        t1_6.setGender(true);  
        t1_6.setAge(25);  
        t1_6.walk();  
        t1_6.eat();  
    }  
}

7).代码阅读:给定如下Java代码,编译运行后,输出结果是什么?并解释原因。
class Base {    
  public Base() {    
       System.out.println("Base");    
  }    
}    
class Child extends Base{    
   public Child() {    
       System.out.println("Child");    
   }    
public class Sample {    
   public static void main(String[] args) {    
      Child c = new Child();    
   }    
}


//--------------先打印父类的 “Base”, 再打印子类的“Child”,子类继承父类的构造方法  



8). 代码改错:请指出如下Java代码中存在的错误,并解释原因。
class Base {    
   public void method() {    
   }    
}    
class Child extends Base{    
   public int method() {    
   }    
   private void method() {    
   }    
   public void method(String s) {    
   }    

}


//--------------子类不能重载父类的方法,只能重写或覆盖、子类中父类的同名方法也不可以私有。  



9). 代码改错:请指出如下Java代码中存在的错误,并改正
public class Sample {    
   public static void main(String[] args) {    
       Child c = new Child();    
   }    
}    
class Base extends Object{    
   private String name;    
   public Base() {    
       name = "Base";    
   }    
}    
class Child extends Base{    
    public Child() {    
       super("Child");    
    }    
}


//-----------------
    //  super("Child");  出错,添加父类的含参构造方法  
    //  public Base(String name) {  
    //      name = "Base";  
    //  }  




10). 代码改错:请指出如下Java代码中存在的错误,并改正。
class Teacher {    
  public void giveLesson() {    
      System.out.println("知识点讲解");    
      System.out.println("总结提问");    
  }    
}    
class DBTeacher extends Teacher {    
  public void giveLesson() {    
      System.out.println("启动 SqlServer");    
      super.giveLesson();    
  }    
  public void sayHi() {    
      System.out.printl ("Hi");    
  }    
}    
public class Test {    
  public static void main(String[] args) {    
      Teacher t = new DBrreacher ();    
      t.sayHi();    
      t.giveLesson();    
}

//-----------------

    //  DBTeacher 向上转型  Teacher,Teacher 的对象不能调用 DBTeacher 的方法,应该为:(代码:Test.java)  
    //  DBTeacher t = new DBTeacher();  

package T1;
//11).在员工管理系统中,有普通员工,经理,董事三种角色,公司所有的员工都有员工Id,
//员工名字,员工基本薪水(2000),请假天数;现初步定Employee类为父类,Manager子类、
//Director(董事)子类,它们的区别是计算工资方式不一样。
//具体工资计算办法:
// A、工资扣除部分:如果请假小于5天,基本工资发75%,大于5天,基本工资发50%  ;
// B、经理的工资=基本工资+住房补贴(基本工资的0.2)+交通补贴(基本工资的0.5)+医疗补贴(500) ;
// C、董事的工资=基本工资+住房补贴(基本工资的0.08)+交通补贴(基本工资的0.3)+医疗补贴(2000)+娱乐补贴(3000) ;
//现完成此系统的设计。
class t1_11 {  
    private  int id;  
    private String name;  
    private double sale;  
    private int qingjia;  
    public t1_11(int id, String name, double sale, int qingjia) {  
        super();  
        this.id = id;  
        this.name = name;  
        this.sale = sale;  
        this.qingjia = qingjia;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public double getSale() {  
        return sale;  
    }  
    public void setSale(double sale) {  
        this.sale = sale;  
    }  
    public int getQingjia() {  
        return qingjia;  
    }  
    public void setQingjia(int qingjia) {  
        this.qingjia = qingjia;  
    }  
      
    public void sale() {  
        if(getQingjia()==0){  
            System.out.println("工资为:"+getSale());  
        }else if(getQingjia()>=0 && getQingjia()<=5){  
            System.out.println("工资为:"+getSale()*0.75);  
        }else if(getQingjia()>5 && getQingjia()<=31){  
            System.out.println("工资为:"+getSale()*0.5);  
        }else{  
            System.out.println("错误");  
        }  
    }  
      
  
}  
class Manager extends t1_11 {  
	  
    public Manager(int id, String name, double sale, int qingjia) {  
        super(id, name, sale, qingjia);  
        // TODO Auto-generated constructor stub  
    }  
    public void sale() {  
        System.out.println("工资为:"+getSale()+getSale()*0.2+getSale()*0.5+500);  
    }  
  
}  


class Director extends t1_11{  
	  
    public Director(int id, String name, double sale, int qingjia) {  
        super(id, name, sale, qingjia);  
        // TODO Auto-generated constructor stub  
    }  
      
    public void sale() {  
        System.out.println("工资为:"+getSale()+getSale()*0.08+getSale()*0.3+2000+3000);  
    }  
  
}  


猜你喜欢

转载自blog.csdn.net/qlwangcong518/article/details/77054095