2017-10-26 5 views
-2

アイスクリームショップのモデルを作成し、開いたカートンの数を追跡し、2つの異なるスクープサイズを許可するアプリケーションを作成するプロジェクトです。私はそれを持っているようにクラスに分割する。Javaが私のオブジェクトを別のものに置き換えているように見える

これは私のスクープクラスを作成して呼び出す主な方法です。

import java.util.Scanner; 
public class Application { 

public static void main(String[] args) { 

    //Creating Objects 
    Scanner input = new Scanner(System.in); 
    Scoop scoop = new Scoop(); 
    IceCreamShoppe icecreamshoppe = new IceCreamShoppe(); 

    //Prompting user to define sizes of scoops and carton 
    System.out.println("How big is the first ice cream scoop?(radius in cm's)"); 
    scoop.setScoop1(input.nextDouble()); 

    System.out.println("How big is the second ice cream scoop?(radius in cm's)"); 
    scoop.setScoop2(input.nextDouble()); 

    System.out.println("How big is the ice cream carton?(radius in cm's)"); 
    icecreamshoppe.setCarton_radius(input.nextDouble()); 

    System.out.println("How big is the ice cream carton?(height in cm's)"); 
    icecreamshoppe.setCarton_height(input.nextDouble()); 


    //Returns to user their set values and sets size of carton 
    System.out.printf("Scoop 1: %.2f, Scoop 2: %.2f, Carton Radius: %.2f, Carton Height %.2f%n", scoop.getScoop1(), scoop.getScoop2(), icecreamshoppe.getCarton_radius(), icecreamshoppe.getCarton_height()); 

    //While statement that continuously prompts user which scoop they want to use, everything else is done in the IceCreamShoppe Class 
    while(true) { 
     System.out.println("Which scoop would you like to use? (1 or 2) (0 = Exit)"); 
     int scoopNumber = input.nextInt(); 
     System.out.println("How many scoops would you like?"); 
     int scoopCount = input.nextInt(); 
     icecreamshoppe.serve(scoopNumber, scoopCount); 
    }//end while 
}//end main 
}//class 

これは自分自身を呼び出す私のスクープメソッドです。私はこれが問題の根源だと思います。問題は、私がスクープクラスを呼び出すたびに私のスクープサイズがリセットされ続けることです。ここで

public class Scoop { 

//Instance Variables 
private double volume; 
private double scoop1; 
private double scoop2; 

//Constructors 
public double doScoop(int r) { 
    System.out.printf("You want this%f", this.getScoop1()); 
if(r == 1) { 
    volume = (Math.PI)*Math.pow(this.getScoop1(), 3)*(4.0/3.0); 
    this.setVolume(volume); 
    System.out.printf("%f", volume); 
    return volume; 
} 
else if(r == 2) { 
    volume = (Math.PI)*Math.pow(this.getScoop2(), 3)*(4.0/3.0); 
    this.setVolume(volume); 
    System.out.printf("%f", volume); 
    return volume; 
    } 
else { 
    System.out.println("Invalid Scoop Number!"); 
    return 0; 
} 
} 



/** 
* @return the volume 
*/ 
public double getVolume() { 
    return volume; 
} 

/** 
* @param volume the volume to set 
*/ 
public void setVolume(double volume) { 
    this.volume = volume; 
} 

/** 
* @return the scoop1 
*/ 
public double getScoop1() { 
    return scoop1; 
} 

/** 
* @param scoop1 the scoop1 to set 
*/ 
public void setScoop1(double scoop1) { 
    this.scoop1 = scoop1; 
} 

/** 
* @return the scoop2 
*/ 
public double getScoop2() { 
    return scoop2; 
} 

/** 
* @param scoop2 the scoop2 to set 
*/ 
public void setScoop2(double scoop2) { 
    this.scoop2 = scoop2; 
} 
// 
}//end class 

は、私は、彼らが問題であると思ういけないが、その後、再び、私は何を知っていますか、他の二つのクラスがありますか?

import java.util.Scanner; 

public class IceCreamShoppe { 

//Instance Variables 
private double carton_radius; 
private double carton_height; 
private int cartons_used = 0; 


//Constructors 
public IceCreamShoppe() { 
} 

public IceCreamShoppe(double r, double h) { 
    this.setCarton_radius(r); 
    this.setCarton_height(h); 
} 

public int carton_used() { 
this.setCartons_used(this.getCartons_used() + 1); 
    return this.getCartons_used(); 
} 

/* 
* Object to serve the ice cream, the only input is which scoop is supposed to be used. 
* @param s Sets the 
*/ 
public boolean serve(int s, int n) { 

    Scanner input = new Scanner(System.in); 
    Scoop scoop = new Scoop(); 
    Carton carton = new Carton(); 
    int i = 0; 
    //Checks which scoop the is being used 
    if(s != 0) { 
     double scoopVolume = scoop.doScoop(1); 
     while(i <= n) { 
      if((boolean) carton.hasEnough(scoopVolume)) { 
       carton.remove(scoopVolume); 
       System.out.println(carton.getContains()); 
       i++; 
      } 
      else { 
       this.carton_used(); 
       carton.carton(this.getCarton_radius(), this.getCarton_height()); 
       carton.remove(scoopVolume); 
       System.out.printf("Carton Empty, Total Cartons Used:%d%n", this.getCartons_used()); 
       System.out.printf("New carton contains: %.2f%n", carton.getContains()); 
       } 
      } 
     } 
    else { 
     System.out.printf("You have used %d cartons of ice cream, there is %.2f ice cream remaining in the current carton", this.getCartons_used(), carton.getContains()); 
     return false; 
    } 
    return true; 
} 

//Getters and Setters 

/** 
* @return the carton_radius 
*/ 
public double getCarton_radius() { 
    return carton_radius; 
} 
/** 
* @param carton_radius the carton_radius to set 
*/ 
public void setCarton_radius(double carton_radius) { 
    this.carton_radius = carton_radius; 
} 
/** 
* @return the carton_height 
*/ 
public double getCarton_height() { 
    return carton_height; 
} 
/** 
* @param carton_height the carton_height to set 
*/ 
public void setCarton_height(double carton_height) { 
    this.carton_height = carton_height; 
} 
/** 
* @return the cartons_used 
*/ 
public int getCartons_used() { 
    return cartons_used; 
} 
/** 
* @param cartons_used the cartons_used to set 
*/ 
public void setCartons_used(int cartons_used) { 
    this.cartons_used = cartons_used; 
} 

}//End Class 




public class Carton { 

//Instance Variables 
private double contains; 

//Objects 

//Checks if the carton has enough remaining, if not 
public boolean hasEnough(double v) { 
    if ((this.getContains() - v) >= 0) { 
     return true; 
    } 
    else { 
     return false; 
    } } 

//Sets the carton size 
public void carton(double r, double h) { 
this.setContains(Math.PI*Math.pow(r, 2)*h); 
} 

//After checking if there is enough ice cream, this will run, there is 
no check against going negative 
public void remove(double v) { 
this.setContains(this.getContains() - v); 
} 

//Getters and Setters 
/** 
* @return the contains 
*/ 
public double getContains() { 
    return contains; 
} 

/** 
* @param contains the contains to set 
*/ 
public void setContains(double contains) { 
    this.contains = contains; 
} 
} 
+5

「私は、これが問題の根本だと思う」 - との問題は何ですか? – alfasin

+1

ようこそStackoverflowへ[How To Ask](https://stackoverflow.com/help/how-to-ask)をお読みください。 [MCVEの作成方法](https://stackoverflow.com/help/mcve)に特に注意してください。良い質問を投稿するために、より多くの努力が必要です。読みやすく、理解しやすく、トピック上にあるものです(https://stackoverflow.com/help/on-topic) - その可能性は高いです関連する人々を引きつけ、より速く助けを得るでしょう。がんばろう! – alfasin

+0

初期化されていない 'Scoop'で' doScoop() 'を実行することはあまりできません... – azurefrog

答えて

0

あなたのicecreamshoppe.serve方法にあなたのスクープオブジェクトを渡していないと、あなたは自分のサーブの方法で新しいスクープオブジェクトを作成しています。

あなたはそれを渡す必要があります。

icecreamshoppe.serve(scoopNumber, scoopCount, scoop); 

と次のように仕える方法を変更します。

public boolean serve(int s, int n, Scoop scoop) { 

    Scanner input = new Scanner(System.in); 

    Carton carton = new Carton(); 
    int i = 0; 
    //Checks which scoop the is being used 
    if(s != 0) { 
     double scoopVolume = scoop.doScoop(1) 
+0

ありがとう、本当にありがとう、このような何かが明らかに見えるかもしれませんが、ちょうど学んでいる誰かに、これは永遠に私を連れて行きました。 comp sciフィールドには喜んで手伝ってくれる人がいます。 :) –

+0

@ThomasWalkerよろしくお願いします! – isaace

関連する問題