Calling a calculated variable from another class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Calling a calculated variable from another class

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    Exclamation Calling a calculated variable from another class

    Hi! I'm having a little bit of a problem with this exercise and I was wondering if anyone could help. here is the problem:

    Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase’s details. Save as Purchase.class
    b. Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a nonnegative value. After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax.

    Here's my code for my Purchase class
    import javax.swing.JOptionPane;
    import java.util.Scanner;

    public class Purchase
    {
    //variables
    public static int invoice;
    public static double saleAmount;
    public static double saleTax;

    //get&set for Invoice
    public void setInvoice(int x)
    {
    invoice = x;
    }
    public int getInvoice( )
    {
    return invoice;
    }

    //get&set for saleAmount
    public void setSaleAmount(double y)
    {
    saleTax = y * 0.05;
    saleAmount = y;
    }
    public double getSaleAmount( )
    {
    return saleAmount;
    }

    //get for saleTax
    public double getSaleTax( )
    {

    return saleTax;
    }

    //display method
    public void display(int invoice, double saleAmount, double saleTax)
    {
    System.out.println("Invoice number: " + invoice + '\n' + "Sale's Amount: " + saleAmount + '\n' + "Sale's Tax: " + saleTax);
    }
    }


    And the code for the CreatePurchase class


    import javax.swing.JOptionPane;
    import java.util.Scanner;

    public class CreatePurchase

    {
    public static void main(String[] args)

    {
    Purchase purchase1 = new Purchase ();

    //scanner for sales amount
    Scanner inputDevice = new Scanner(System.in);
    System.out.println("Please enter the sale amount: ");
    Purchase.saleAmount = inputDevice.nextDouble();

    //loop for saleAmount
    while (Purchase.saleAmount < 1)
    {
    System.out.print('\n'+ "Error, your sale amount needs to be more than 0. Please enter a valid sale amount: >> ");
    Purchase.saleAmount = inputDevice.nextDouble();
    }

    //scanner for invoice
    System.out.println("Please enter an invoice number between 1000 and 8000: ");
    Purchase.invoice = inputDevice.nextInt();

    //loop for invoice
    while (Purchase.invoice < 999 || Purchase.invoice > 8000)
    {
    System.out.print('\n'+ "Error, please enter a valid invoice number between 1000 and 8000: >> ");
    Purchase.invoice = inputDevice.nextInt();
    }

    //display result
    JOptionPane.showMessageDialog(null, "Your invoice number is " + Purchase.invoice + '\n'
    + "Your sale tax is: " + Purchase.saleTax + '\n'
    + "Your grand total is: " + Purchase.saleAmount);

    }
    }


    As you can see, when you run the second class, the saleAmount doesn't include the extra 5% for sale tax and sale tax remains as 0. Is probably something really silly, but I have no idea where to start.

  2. #2
    Join Date
    Jun 2011
    Posts
    7

    Re: Calling a calculated variable from another class

    Make your variables non static, call these variables with a "this." keyword from within the class. You are not using the getter and setter methods within the Purchase class and at the moment you are using the Purchase class to only hold two static variables and the sales tax does not get calculated. Use debugger to track values at execution time, or use http://www.cs.joensuu.fi/jeliot/
    It should be something like that (far from being ideal)

    Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class CreatePurchase
    
    {
    
    public static void main(String[] args)
    
    {	
    
    //scanner for sales amount	
    Scanner inputDevice = new Scanner(System.in);
    System.out.println("Please enter the sale amount: ");
    Purchase purchase1=new Purchase(inputDevice.nextDouble());
    //loop for saleAmount
    while (purchase1.getSaleAmount() < 1)
    {	
    System.out.print('\n'+ "Error, your sale amount needs to be more than 0. Please enter a valid sale amount: >> ");
    purchase1.setSaleAmount(inputDevice.nextDouble());
    }
    
    //scanner for invoice 
    System.out.println("Please enter an invoice number between 1000 and 8000: ");
    purchase1.setInvoicenr(inputDevice.nextInt()); 
    //loop for invoice
    while (purchase1.getInvoicenr() < 999 || purchase1.getInvoicenr() > 8000)
    {
    System.out.print('\n'+ "Error, please enter a valid invoice number between 1000 and 8000: >> ");
    purchase1.setInvoicenr(inputDevice.nextInt());
    }
    //display result
    purchase1.display();
    JOptionPane.showMessageDialog(null, "Your invoice number is " + purchase1.getInvoicenr() + '\n' 
    + "Your sale tax is: " + purchase1.getSaleTax() + '\n'
    + "Your grand total is: " + purchase1.getTotal());	
    
    }
    }

    Code:
    import java.util.Scanner;
    
    public class Purchase
    {
    //variables
    private int invoicenr;
    private double saleAmount;
    private double saleTax;
    private double total;
    //constructor
    public Purchase(double amount){
    this.saleAmount=amount;
    this.saleTax=amount*0.05;
    this.total=amount+amount*0.05;
    }
    //get&set for Invoice 
    public void setInvoicenr(int x)
    {
    invoicenr = x;
    }
    public int getInvoicenr( )
    {
    return this.invoicenr;
    }
    //get&set for saleAmount
    public void setSaleAmount(double y)
    {
    this.saleAmount = y;
    this.saleTax=y*0.05;
    this.setTotal(y*1.05);
    }
    public double getSaleAmount( )
    {
    return saleAmount;
    }
    public double getTotal(){
    return this.total;
    }
    public void setTotal(double t){
    this.total=t;
    }
    //get for saleTax
    public double getSaleTax( )
    {
    return this.saleAmount*0.05;
    }
    
    //display method
    public void display()
    {
    System.out.println("Invoice number: " + this.invoicenr + '\n' + "Sale's Amount: " + this.saleAmount + '\n' + "Sale's Tax: " + this.saleTax);
    }
    }

  3. #3
    Join Date
    Jun 2013
    Posts
    2

    Re: Calling a calculated variable from another class

    Thanks! I'm very new at this and still trying to understand the "this" function but I'll get there! Thanks you so much!

    Quote Originally Posted by khaahk View Post
    Make your variables non static, call these variables with a "this." keyword from within the class. You are not using the getter and setter methods within the Purchase class and at the moment you are using the Purchase class to only hold two static variables and the sales tax does not get calculated. Use debugger to track values at execution time, or use http://www.cs.joensuu.fi/jeliot/
    It should be something like that (far from being ideal)

    Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class CreatePurchase
    
    {
    
    public static void main(String[] args)
    
    {	
    
    //scanner for sales amount	
    Scanner inputDevice = new Scanner(System.in);
    System.out.println("Please enter the sale amount: ");
    Purchase purchase1=new Purchase(inputDevice.nextDouble());
    //loop for saleAmount
    while (purchase1.getSaleAmount() < 1)
    {	
    System.out.print('\n'+ "Error, your sale amount needs to be more than 0. Please enter a valid sale amount: >> ");
    purchase1.setSaleAmount(inputDevice.nextDouble());
    }
    
    //scanner for invoice 
    System.out.println("Please enter an invoice number between 1000 and 8000: ");
    purchase1.setInvoicenr(inputDevice.nextInt()); 
    //loop for invoice
    while (purchase1.getInvoicenr() < 999 || purchase1.getInvoicenr() > 8000)
    {
    System.out.print('\n'+ "Error, please enter a valid invoice number between 1000 and 8000: >> ");
    purchase1.setInvoicenr(inputDevice.nextInt());
    }
    //display result
    purchase1.display();
    JOptionPane.showMessageDialog(null, "Your invoice number is " + purchase1.getInvoicenr() + '\n' 
    + "Your sale tax is: " + purchase1.getSaleTax() + '\n'
    + "Your grand total is: " + purchase1.getTotal());	
    
    }
    }

    Code:
    import java.util.Scanner;
    
    public class Purchase
    {
    //variables
    private int invoicenr;
    private double saleAmount;
    private double saleTax;
    private double total;
    //constructor
    public Purchase(double amount){
    this.saleAmount=amount;
    this.saleTax=amount*0.05;
    this.total=amount+amount*0.05;
    }
    //get&set for Invoice 
    public void setInvoicenr(int x)
    {
    invoicenr = x;
    }
    public int getInvoicenr( )
    {
    return this.invoicenr;
    }
    //get&set for saleAmount
    public void setSaleAmount(double y)
    {
    this.saleAmount = y;
    this.saleTax=y*0.05;
    this.setTotal(y*1.05);
    }
    public double getSaleAmount( )
    {
    return saleAmount;
    }
    public double getTotal(){
    return this.total;
    }
    public void setTotal(double t){
    this.total=t;
    }
    //get for saleTax
    public double getSaleTax( )
    {
    return this.saleAmount*0.05;
    }
    
    //display method
    public void display()
    {
    System.out.println("Invoice number: " + this.invoicenr + '\n' + "Sale's Amount: " + this.saleAmount + '\n' + "Sale's Tax: " + this.saleTax);
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center