2017-10-18 6 views
0

キーボード入力を取得し、それをProductionWorker1クラスから作成したオブジェクトに渡す必要があります。 ProductionWorker1クラスは、Employee1クラスを拡張して、名前、番号、および日付(採用者)を取得します。 ProductionWorker1クラスには、shiftとpayを持つデフォルトのコンストラクタがあります。 Employee1コンストラクターには、名前、番号、および日付があります。もし誰かが私が本当にそれを感謝するのを助けることができます。ProductionWorker1クラスからオブジェクトを作成し、セッターとゲッターを使用する

public class Employee1 
{ 
    public Employee1(String name) 
    { 
    System.out.println("name: " + name); 
    } 
    public void setName(String name) 
    { 
     name = name; 
    } 
    public String getName(String name) 
    { 
     return name; 
    } 
} 

public class ProductionWorker1 extends Employee1 
{ 

    public ProductionWorker1(int shift, double pay) 
    { 
    super(""); 
    System.out.println("shift: " + shift); 
    System.out.println("pay: " + pay); 

    } 
    public void setShift(int shift) 
    { 
    shift = shift; 
    } 
    public int getShift(int shift) 
    { 
    return shift; 
    } 

} 

import java.util.Scanner; 

public class Main 
{ 
    public static void main(String[] s) 
    { 
    String name; 
    String shift; 
    String pay; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("enter name"); 
    name = keyboard.nextLine(); 
    System.out.println("enter shift time"); 
    shift = keyboard.nextLine(); 
    System.out.println("enter pay"); 
    pay = keyboard.nextLine(); 

    ProductionWorker1 obj = new ProductionWorker1(obj.getName()); 
    } 
} 
+0

???メインの最後の行をチェックしてください。オブジェクトを設定する前に、クラスを新しいインスタンスに設定し、そのオブジェクトの名前のコンストラクタを送信しています。 String nameを渡すつもりはありませんか? –

+0

@olderコーダー私は変数shiftを変更してintにし、doubleをdoubleにしてobjに渡します: int shift; 二重支払い。 ProductionWorker1 obj =新しいProductionWorker1(int、shift); これが機能しました。他のクラスのセッターやゲッターの使い方を知っていますか?私はMainの変数を使っているだけなので? –

+0

'Employee1'コンストラクタは名前、番号、日付を取りますが、名前だけを取るとします。また、クラスには属性がありません。 – alejandrogiron

答えて

0

これは、あなたが開始されます。申し訳ありませんが、私は横行していました。メインでは、文字列をintに変換する方法を見つける。お支払いと同じです。彼らのタイプとして単にスキャンする方がいいでしょう。

/** 
    * You have setters and getters, but you don't have any class variables to set/get 
    * @author me 
    * 
    */ 

    public class Employee1 { 
    // You don't have any class variables. 
    // I added this one. 
     String name; 

     public Employee1(String name) { 
       // I added this line to set the class variable name to parameter name 
       setName(name); 
       System.out.println("In Constructor Employee1(String) name: " + name); 
     } 

     public void setName(String name) { 
     // I changed this to set the class name to the parameter name 
       this.name = name; 
     } 

     /** 
     * IF You are getting the class variable, you don't need to pass a variable to get it. 
     * @return 
     */ 
     public String getName() { 
       return name; 
     } 
    } 


public class ProductionWorker1 extends Employee1 { 

// Since ProductionWorker1 extends Employee, it will 
// have a name variable. Now add shift and pay for ProductionWorker1 
int shift = 0; 
double pay = 0.0; 

/** 
* Constructor with NO NAME!!! 
* @param shift 
* @param pay 
*/ 
public ProductionWorker1(int shift, double pay) { 
    super(""); // initializes Employee1 and sets name to empty string 
    setShift(shift); 
    setPay(pay); 
    System.out.println("In Constructor ProductionWorker1(int,double)shift: " + shift); 
    System.out.println("pay: " + pay); 

} 

/** 
* I added this Constructor to get the name down to Employee1 
* @param name 
* @param shift 
* @param pay 
*/ 
public ProductionWorker1(String name, int shift, double pay) { 
    super(name); // initializes Employee1 and sets name to ProductionWorker's name 
    setShift(shift); 
    setPay(pay); 
    System.out.println("In Constructor ProductionWorker1(String int,double)shift: " + shift); 
    System.out.println("pay: " + pay); 
} 

public double getPay() { 
    return pay; 
} 
public void setPay(double pay) { 
    this.pay = pay; 
} 

public void setShift(int shift) { 
    this.shift = shift; 
} 

public int getShift() { 
    return shift; 
} 

} 



public class Main { 

public static void main(String[] args) { 
    String name; 
    String shift; 
    String pay; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("enter name"); 
    name = keyboard.nextLine(); 
    System.out.println("enter shift time"); 
    shift = keyboard.nextLine(); 
    System.out.println("enter pay"); 
    pay = keyboard.nextLine(); 

    // !!! You need to create an int from a string OR you could scan it in 
    // as an integer. For now, I'm using a temp int. 
    int tempShift = 5; 
    // same with pay 
    double tempPay = 12.56; 

    ProductionWorker1 obj = new ProductionWorker1(name, tempShift, tempPay); 
    // obj dude got a raise 
    obj.setPay(24.75); 

} 
関連する問題