distro
distro

Reputation: 41

Need Help Compiling Java (Accessors, Mutators, Constructors)

So I have been working on this project for some time now and I'm having a lot of trouble grasping Java for some reason.

The objective is to create 3 objects that contain 3 fruits each and each fruit has its own price/value.

I'm having trouble adding up the values currently, I'm sure there is much more wrong as I said I'm having a lot of trouble with Java so far.

My biggest problem is currently the costofBox() method.

Any suggestions that are helpful would be greatly appreciated, I've been working on this for over a week now.

Here is the entire program:

public class Project8
{

private String fruit1;
private String fruit2;
private String fruit3;
private String Bundle1;
private String Bundle2;
private String Bundle3;
private int costofBox;
double total;
int broccoli;
int tomato;
int kiwi;
int kale;
int orange;

public String toString()
{
    String output = "The box contains: " + Bundle1 + ", " + Bundle2 + ", " + Bundle3 +
    "and the cost is $" + costofBox();
    return output;
}

public String getBundle1()
{
    return Bundle1;
}
public String getBundle2()
{
    return Bundle2;
}
public String getBundle3()
{
    return Bundle3;
}


public void setBundle1(String Bundle1) 
{
    Bundle1=fruit1;
}
public void setBundle2(String Bundle2) 
{
    Bundle2=fruit2;
}
public void setBundle3(String Bundle3) 
{
    Bundle3=fruit3;
}

public double costofBox()
{
    double total=0;
    if(Bundle1.equals("broccoli"))
        total+=6;
    else if(Bundle1.equals("tomato"))
        total+=5;
    else if(Bundle1.equals("kiwi"))
        total+=8;
    else if(Bundle1.equals("kale"))
        total+=4;
    else if(Bundle1.equals("orange"))
        total+=7;
    if(Bundle2.equals("broccoli"))
        total+=6;
    else if(Bundle2.equals("tomato"))
        total+=5;
    else if(Bundle2.equals("kiwi"))
        total+=8;
    else if(Bundle2.equals("kale"))
        total+=4;
    else if(Bundle2.equals("orange"))
        total+=7;
    if(Bundle3.equals("broccoli"))
        total+=6;
    else if(Bundle3.equals("tomato"))
        total+=5;
    else if(Bundle3.equals("kiwi"))
        total+=8;
    else if(Bundle3.equals("kale"))
        total+=4;
    else if(Bundle3.equals("orange"))
        total+=7;

    return total;
}

public Project8()
{    
    fruit1 = "broccoli" + "kale" + "orange";
    fruit2 = "kale" + "kiwi" + "orange";
    fruit3 = "broccoli" + "tomato" + "kiwi";
}

public Project8(String fruit1, String fruit2, String fruit3)
{
    String Bundle1=fruit1;
    String Bundle2=fruit2;
    String Bundle3=fruit3;
}

public static void main (String [] args)
{
    Project8 Bundle1=new Project8 ("broccoli", "kale", "orange");
    Project8 Bundle2=new Project8 ("kale", "kiwi", "orange");
    Project8 Bundle3=new Project8 ("broccoli", "tomato", "kiwi");



    System.out.println("Week 1: " + Bundle1.toString());
    System.out.println("Week 2: " + Bundle2.toString());
    System.out.println("Week 3: " + Bundle3.toString());
    System.out.println("Week4: The box contains:,, and the cost is $0.0");
    }
}

Thank you ahead of time for those of you who can help me out!

Upvotes: 0

Views: 181

Answers (1)

Michael
Michael

Reputation: 44150

Your problem is in this constructor:

public Project8(String fruit1, String fruit2, String fruit3)
{
    String Bundle1=fruit1;
    String Bundle2=fruit2;
    String Bundle3=fruit3;
}

Because of the type String in front of these assignments, you are declaring new local variables! This means that the fields of your class:

private String Bundle1;
private String Bundle2;
private String Bundle3;

... are not ever given values. When you try to access them you get the exception you were seeing because they are NULL.

If you change the constructor to:

public Project8(String fruit1, String fruit2, String fruit3)
{
    Bundle1 = fruit1;
    Bundle2 = fruit2;
    Bundle3 = fruit3;
}

then your project will execute properly.


As an aside, there's a lot of ways you could reduce the length of your program, make things more concise, and repeat yourself less. I suggest once you've got it working you head over to Code Review which is a sister site to StackOverflow: they'll give you suggestions to improve. If you do decide to do that, drop me a comment on this answer!

Upvotes: 3

Related Questions