Question

I am really struggling with get/set methods. I understand the basic concept - first you set the value then you retrieve it. I am finding it rather difficult to find information about it with the minimal concepts I have already learned. I am on the 6th chapter or my first Java and programming class and it is all online. I created a couple other classes that used set/get methods, but those don't really seem to fit this project.

public class Purchase{
   int inv;
   double sale;
   double tax;
   double taxAmount = 0.05;

public int getInv()
{
   return inv;
}
public void setInv(int inv)
{
   inv = inv;
}

public void setSale(double s)
{
   sale = s;
   tax = sale * taxAmount;
}
public double getSale()
{
   return sale;
}

//display
public void display()
{
System.out.println("Invoice number: " + getInv() + "\nSale amount: $" + getSale() + "\nTax: $" + tax + "\nTotal: $" + (tax + sale));
}
}

import java.util.Scanner;
public class CreatePurchase{
public static void main(String[] args)
{
   Purchase one = new Purchase();

   Scanner input = new Scanner(System.in);
   do
   {
   System.out.print("Please enter you invoice number. This will be a number between 1000 and 8000.  ");
   inv = input.nextInt();
   one.setInv(inv);
   }
   while(inv < 1000 && inv > 8000);
   {
   System.out.println("Please enter the amount of your sale.  ");
   sale = input.nextInt();
   }
}
}

The CreatePurchase class is not finished, but I compile it and get the below for every variable every time it appears:

CreatePurchase.java:16: error: cannot find symbol
      inv = input.nextInt();
      ^

It was my understanding that a default constructor is made, so I didn't add one, but called it in CreatePurchase. Any suggestions?

Was it helpful?

Solution 2

You have at least two problems. First, you can create a new variable with the same name as another variable that's in a bigger container (scope). In this case, the new variable "hides" or "shadows" the outer one. In your setInv method, when you say inv = inv, both invs refer to the innermost variable, the one in the method signature. To save the argument to the class's field, you need to specify the outer inv: this.inv = inv;.

In your CreatePurchase class, you don't have any inv defined; there's one in Purchase, but that's over there, not here. You just need to declare int inv; right after your Purchase one.

Based on these two errors, I would recommend reading an article or tutorial about variable scope in Java to learn the rules about which variables are accessible where.

OTHER TIPS

You've failed to declare the variable inv any where, for example...

public static void main(String[] args) {
    Purchase one = new Purchase();

    // !! Variable must be declared before it can be used !! //
    int inv = 0;
    Scanner input = new Scanner(System.in);
    do {
        System.out.print("Please enter you invoice number. This will be a number between 1000 and 8000.  ");
        inv = input.nextInt();
        one.setInv(inv);
    } while (inv < 1000 && inv > 8000);
    {
        System.out.println("Please enter the amount of your sale.  ");
        // This will be your next problem...
        sale = input.nextInt();
    }
}

Your Purchase class is also going to have problems...

The following method is essentially assigning the value of inv back to itself, which is meaningless in this context...

public void setInv(int inv)
{
    inv = inv;
}

Instead, you should be assigning to the instance of the Purchase class's inv variable...

public void setInv(int inv)
{
    this.inv = inv;
}

You havent declared the variable inv in main method, at this step inv = input.nextInt(); Change your program to below

int inv = 0;
 Scanner input = new Scanner(System.in);
   do
   {
   System.out.print("Please enter you invoice number. This will be a number between 1000 and 8000.  ");
   inv = input.nextInt();
   if(inv >1000 & inv <8000)
        one.setInv(inv);//add to one variable only if its correct,otherwise ignore it
   }
   while(inv < 1000 && inv > 8000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top