Results 1 to 2 of 2

Thread: class question

  1. #1
    Maverick83 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default class question

    hi guys ,

    i am new to java and trying to learn .so i have a question about classes.

    Java Code:
     /* when i use this way its asking me 3 times what is amountOfPurchase
     i want to declare amountOfPurchase as a public variable and use it in all classes
    
    or how i can prevent to asking 3 times?or i can get the input of amountOfPurchase in the main class and use it in all classes but i dont know how to do it?or do you  guys have any  idea?
       */
    
    
    import java.util.Scanner;
    public class Tax
    {
    
    
    
    public static void main(String [] args)
    
      {
     
    	
     
       Tax tax=new Tax();
    	
    	tax.EnterInput();
    	tax.stateSalesTax();
       tax.countySalesTax();
    
    
    	  
      
      }
    
     public double EnterInput()
     
     {
        double amountOfPurchase;
    			Scanner sr=new Scanner(System.in);
    			System.out.print("Enter Amount Of Purchase : ");
    			amountOfPurchase=sr.nextDouble();
    			
    		   return amountOfPurchase;
     	  
     
     }  
    
    public void  stateSalesTax()
    
     {
          double amountOfPurchase,stateSalesTax;
    		amountOfPurchase=EnterInput();
    		stateSalesTax= (amountOfPurchase*40/100);
          System.out.println("The State Sales Tax is : "+stateSalesTax);
     
     }
     
     
     public void countySalesTax()
     
     {
     
          double amountOfPurchase,countySalesTax;
    		
    		amountOfPurchase=EnterInput();
    
    		countySalesTax=(amountOfPurchase*20/100);
    
    		System.out.println("The County Sales Tax is : "+countySalesTax);
    
     
      
     }
     
    
     
    
    }

    thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Friendly suggestions: Please ask your question in the forum, not as a comment in your code so as to make it easier to find and read. Also, you'll want to use non-random code indentation because if your code is difficult to read, no one will want to read it decreasing your chances of getting help.

    e.g.,


    "My current program is requesting that I enter amountOfPurchase three times. Instead I want to declare amountOfPurchase as a public variable, use it in all methods, and not be prompted to enter it more than once."

    Java Code:
    import java.util.Scanner;
    
    public class Tax {
       public static void main(String[] args) {
          Tax tax = new Tax();
          tax.EnterInput();
          tax.stateSalesTax();
          tax.countySalesTax();
       }
    
       public double EnterInput() {
          double amountOfPurchase;
          Scanner sr = new Scanner(System.in);
          System.out.print("Enter Amount Of Purchase : ");
          amountOfPurchase = sr.nextDouble();
          return amountOfPurchase;
       }
    
       public void stateSalesTax() {
          double amountOfPurchase, stateSalesTax;
          amountOfPurchase = EnterInput();
          stateSalesTax = (amountOfPurchase * 40 / 100);
          System.out.println("The State Sales Tax is : " + stateSalesTax);
       }
    
       public void countySalesTax() {
          double amountOfPurchase, countySalesTax;
          amountOfPurchase = EnterInput();
          countySalesTax = (amountOfPurchase * 20 / 100);
          System.out.println("The County Sales Tax is : " + countySalesTax);
       }
    }

    So what happens when you try creating an amountOfPurchase double variable, have it set from within the EnterInput() method and only call that method once, from within the main method?
    Last edited by Fubarable; 06-19-2011 at 07:46 AM.

Similar Threads

  1. Class and constructors question.
    By Meta in forum New To Java
    Replies: 19
    Last Post: 04-29-2010, 02:58 AM
  2. [SOLVED] Class question
    By Supamagier in forum Advanced Java
    Replies: 19
    Last Post: 05-12-2009, 05:11 PM
  3. I have some question about jar and class??
    By low224 in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 04:02 AM
  4. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 12:41 AM
  5. question regarding class
    By kavithas in forum New To Java
    Replies: 4
    Last Post: 11-16-2007, 09:12 AM

Posting Permissions

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