Results 1 to 4 of 4

Thread: get/set

  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default get/set

    I've created a class (haven't touched it in a wk) but I'm not sure how the set and get method work. I know you have to call them from the application (the set method first) but what do you do with the getMethods?

    I'm not sure if posting my code will help, but here it is:

    Java Code:
    import javax.swing.JOptionPane;
    import java.text.*;
    import java.util.*;
    public class CandyClass
    {
      private double dumdums;
      private double snickerballs;
      private double sweettarts;
      private double skittles;
      private final  Double tax;
      private double subtotal;
      private double total;
      private double price;
      private int qtyWanted;
      private int quantity;
      //private double order1, order2, order3;
      private DecimalFormat decimalf = new DecimalFormat("$##.##");
      private NumberFormat money = NumberFormat.getCurrencyInstance(Locale.US);
      
        public CandyClass()
        {
          dumdums = 1.50;
          snickerballs = 3.00;
          sweettarts = 2.00;
          skittles = 2.75;
          tax = .06;
        }
        
        //Mutators: set/calculate methods
        public void setDumdums(double inDumdums)
        {
          dumdums = inDumdums;
        }
        public void setSnickerballs(double inSnickerballs)
        {
          snickerballs = inSnickerballs;
        }
        public void setSweettarts(double inSweettarts)
        {
          sweettarts = inSweettarts;
        }
        public void setSkittles(double inSkittles)
        {
          skittles = inSkittles;
        }
        public void setQuantity(int inQuantity)
        {
          quantity = inQuantity;
        }
        public double calcPrice(double candy, int quantity) // calculates a single order
        {
          price = candy * quantity;
          return price;
        }
        
         public double calcTotal(double one, double two, double three) //calculates the total order, which will include taxes
        {
          subtotal = (one + two + three) * tax;
          total = one + two + three + subtotal;
          return total;
        }
        
        //Accessors: get/display methods
         public double getDumdums() 
        {
         return dumdums;
        }
        public double getSnickerballs()
        {
        return snickerballs;
        }
        public double getSweettarts()
        {
          return sweettarts;
        }
        public double getSkittles()
        {
          return skittles;
        }
        public int getQuantity() //gets how many pounds a customer wants
        {
          return qtyWanted;
        }
         public void displayMsg()
          {
              JOptionPane.showMessageDialog(null, "Tax for all items is: " + decimalf.format(tax));
              JOptionPane.showMessageDialog(null,"Your total is: " + money.format (total));
              JOptionPane.showMessageDialog(null, "Thank you for shopping at CandyLand!\n\t"
                                               + "Tell a friend!");
          }
      }

  2. #2
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I am not sure if I know exactly what you are trying to ask, but the setters allow you to change the value of one of your instance variables and the getters allow you to get the instance variables current value

  3. #3
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    so anytime you call the setMethod, you should immediately call the getMethod?
    from there the getMethod takes to value in the setMethod which can then be used to pass into my calcMethod?

  4. #4
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by smray7 View Post
    so anytime you call the setMethod, you should immediately call the getMethod?
    from there the getMethod takes to value in the setMethod which can then be used to pass into my calcMethod?
    No, you have your getters and setters set up properly. here is an example of how you might use them

    Java Code:
    public static void main(String[] args)
    {
    	CandyClass exampleCandy = new CandyClass();
    	
    	System.out.println(exampleCandy.getDumdums()); //will output 1.5
    
    	exampleCandy.setDumdums(5);
    	System.out.println(exampleCandy.getDumdums()); //will output 5.0
    	
    }
    Last edited by Bertstar; 12-08-2010 at 11:24 PM.

Posting Permissions

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