【雪野實訓記錄】Java-T1作業 基礎
阿新 • • 發佈:2018-12-01
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);
}
}