Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Instance variable inside a method

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Instance variable inside a method

    I am working on a project where I need to return 3 values I understand I can only return one using a method alone! So I need to add 2 instance variables, that will return the other 2 values. Can someone please assist me in this? Here is my code, it may be completely wrong! I have used a larger font and commented as much as possible about where I am having a problem!
    import java.util.Scanner;
     
    // The Bandwidth class
     
     
    public class WebHostOrder14 {
     
        private double highestCharge; // instance variable for highestCharge
        private double lowestCharge; // instance variable for lowestCherge
     
        private Scanner scanner;
     
        //create a new constructor to do the work
        public WebHostOrder14() {
            WebHost webHost;
            double average;
            double shipping;
            double highest;
            double lowest;
     
            System.out.println("\nWelcome to the Web Host Ordering System\n");
            scanner = new Scanner(System.in);
            average = computeAverageSales();
            System.out.printf("\nThe average sales for these packages is: $" + average);
            if (average >= 200) {
                System.out.println("\nWow! Those sales are awesome!!\n");
            }
            shipping = computeShippingCharges();
             System.out.printf("\nThe shipping charges total up to $" + shipping);
        }
     
        private double computeAverageSales() {
            WebHost webHost;
            double average;
            double total = 0;
            int counter = 0;
     
            while (true) {
                System.out.printf("Please enter the price of the new package: $");
                double webHostPrice = scanner.nextDouble();
                total = webHostPrice + total;
                counter++;
                if (webHostPrice == 0 || webHostPrice == 0.00) {
                    break;
                } else if (webHostPrice <= -1) {
                    System.out.println("Please, enter a positive number!");
                } else if (webHostPrice > 500) {
                    System.out.println("That is unrealistic, please enter the correct amount!");
                } else if (webHostPrice >=1  || webHostPrice <= 500){
                System.out.printf("You entered price $" + webHostPrice);
                }// end webHostPrice validation
     
                System.out.print("What will be the product ID: ");
                int webHostId = scanner.nextInt();
                scanner.nextLine();
                if (webHostId <= 0) {
                    System.out.println("Please, enter a positive number!");
                } else if (webHostId > 10000) {
                    System.out.println("Please enter the correct produce ID!");
                } // end webHostId validation statements
     
                System.out.print("What is the product name: ");
                String webHostName = scanner.next();
                if (webHostName == null){
                    System.out.println("Please enter a valid product name!");
                }// end webHostName validation
     
                System.out.print("Please enter the required Bandwidth needed for your site? ");
                double webHostBandwidth = scanner.nextDouble();
                if (webHostBandwidth <= 0){
                    System.out.println("Please enter a positive number!");
                } else if (webHostBandwidth > 1000000){
                    System.out.println("That seems high are you sure?");
                } // end webHostBandwidth validation
     
                System.out.print("How many FTP accounts do you need? ");
                int webHostFtp = scanner.nextInt();
                if (webHostFtp <= 0){
                    System.out.println("Please enter a positive number!");
                } else if (webHostFtp > 100){
                    System.out.println("Please enter a valid number!");
                } // emd webHostFtp validation
     
                System.out.print("How many email accounts do you need? ");
                int webHostMail = scanner.nextInt();
                if (webHostMail <= 0){
                    System.out.print("Please enter a positive number!");
                } else if (webHostMail > 500){
                    System.out.println("Please enter a valid number!");
                } // end webHostMail validation
     
                System.out.print("What is your domain name? ");
                String webHostDomain = scanner.next();
                if (webHostDomain == null){
                    System.out.println("Please enter a valid domain!");
                } // end webHostDomain validation
            } // end while statement
        average  = total / (counter - 1);
        return average;
    } // end computeAverageSales
     
     
       [SIZE=4] /*Here is where my problem is*/[/SIZE]
    private double computeShippingCharges() {
        WebHost webHost;
            double shipping = 0;
            double highest;
            double lowest;
            double x = 0;
            double y = 0;
     
     
            System.out.println("\nWelcome to the Shipping Wizard!\n");
            System.out.println("\nEnter 0 to end!\n");
     
     
     
           // while (true) {
                // Acquire a price. A price == 0 indicates end-of-data
     
                System.out.print("Please enter shipping charges: $");
                double charges = scanner.nextDouble();
                charges = x;
                if (charges < x){
                    highest = x;
                    System.out.printf("The highest sales price added was $" + highest);
                } else if (charges > x){
                    x= y;
                    lowest = y;
                    System.out.print("The lowest value was $" + lowest);
                }
     
                // Test for end-of-data
               /*  if (charges <= -1) {
                    System.out.println("Please enter a positive number!");
                    charges = scanner.nextDouble();
                } else if (charges > 1000) {
                    System.out.println("That is unrealistic please enter the correct value!");
                    charges = scanner.nextDouble();
                } else if (charges == 0){
                    break;
                }
     
     
     
            }*/
     
            return shipping;
    }
     
        public static void main(String[] args) {
            WebHost webHost = new WebHost();
            new WebHostOrder14();
        } // end main
    } // end program
    Here is my separate class
    class WebHost {
     
        private double bandwidth, price, [SIZE=4]highest, lowest[/SIZE];
        private int ftp, mail, id;
        private String domain, name;
     
        // Constructors
        WebHost() {
            setWebHostBandwidth(0.0);
            setWebHostPrice(0.0);
            [SIZE=4]setWebHostHighest(0.0); //// added this////
            setWebHostLowest(0.0); //// and added this////[/SIZE]
            setWebHostFtp(0);
            setWebHostMail(0);
            setWebHostId(0);
            setWebHostDomain("");
            setWebHostName("");
     
        } // End WebHost
     
        WebHost(double bandwidth, double price, [SIZE=4]double highest, double lowest[/SIZE], int ftp, int mail, int id,
                String domain, String name) {
            setWebHostBandwidth(bandwidth);
            setWebHostPrice(price);
            [SIZE=4]setWebHostHighest(highest);
            setWebHostLowest(lowest);[/SIZE]
            setWebHostFtp(ftp);
            setWebHostMail(mail);
            setWebHostId(id);
            setWebHostDomain(domain);
            setWebHostName(name);
     
        } // end WebHost
     
        //Getters and Setters
        public double getWebHostBandwidth() {
            return bandwidth;
        } //end getWebHostBandwidth
     
        public double getWebHostPrice() {
            return price;
        } //end getWebHostPrice
        [SIZE=4]////added the next 2 sections of code below//// 
        public double getWebHostHighest(){
            return highest;
        }
     
        public double getWebHostLowest(){
            return lowest;
        }
    [/SIZE]
        public int getWebHostFtp() {
            return ftp;
        } //end getWebHostFtp
     
        public int getWebHostMail() {
            return mail;
        } //end getWebHostMail
     
        public int getWebHostId() {
            return id;
        } //end getWebHostId
     
        public String getWebHostDomain() {
            return domain;
        } //end getWebHostDomain
     
        public String getWebHostName() {
            return name;
        } //end getWebHostName  
     
        public final void setWebHostBandwidth(double bandwidth) {
            this.bandwidth = bandwidth;
        } //end setBandwidth
     
        public final void setWebHostPrice(double price) {
            this.price = price;
        }//end setPrice
        [SIZE=4]//// again I added the next 2 sections below as well//////
        public final void setWebHostHighest(double highest){
            this.highest = highest;
        }
     
        public final void setWebHostLowest(double lowest){
            this.lowest = lowest;
        }
    [/SIZE]
        public final void setWebHostFtp(int ftp) {
            this.ftp = ftp;
        } //end setFtp
     
        public final void setWebHostMail(int mail) {
            this.mail = mail;
        } //end setMail
     
        public final void setWebHostId(int id) {
            this.id = id;
        } //end setId
     
        public final void setWebHostDomain(String domain) {
            this.domain = domain;
        } //end setDomain
     
        public final void setWebHostName(String name) {
            this.name = name;
        } //end setName
     
        // Display WebHost data
         public void display() {
         System.out.println("\nThank you for your order you ordered a package "
         + "with\n" + "Bandwidth: " + getWebHostBandwidth() + " Mbits\n"
         + getWebHostFtp() + " FTP accounts\n" + getWebHostMail()
         + " mail accounts\n" + "For the domain of\n " + getWebHostDomain());
         System.out.println("This package has an ID of: " + getWebHostId()
         + "/nThe package name is: " + getWebHostName() + "/nThe price is set at: $"
         + getWebHostPrice());
         } // end display()
    } // end class WebHost
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    I need to add 2 instance variables, that will return the other 2 values.
    What values in what variables in what class do you want methods in other classes to have access to?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    In the computeShippingCharges() method I need to display total shipping, highest charge, and the lowest charge. I added to the WebHost() class I do not know if that is correct I am lost on this one!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    I need to display total shipping, highest charge, and the lowest charge
    When you say display, do you mean to use the println() statement to display the values on the console?

    What are the names of the variables with the values you want to display? What class are they in?
    Does the class with the computeShippingCharges() method have a reference to that class so the computeShippingCharges() method can access the variables you need to get to?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    Yes as far as I understand I need to use printf() to display the output. I need to display the highest input value and the lowest inputed value, also I need to display the accumulated total of the inputs. If I am not mistaking this is the reference to class? WebHost webHost; I may be wrong I new to Java.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    What variables hold the values you want to print in the computeShippingCharges() method?
    What class are those variables located in?

    Does the class that the computeShippingCharges() method is in have a reference to the class that contains those variables?

    How did you get a 260+ line program without first testing the technique of how one class can get access to the variables in another class?
    Have you tried writing a couple of classes with about 30 lines of code with variables and methods to test the techniques that are giving you a problem in the posted code? There is a lot of code here that is not part of your problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Instance variable inside a method

    Now also appearing at java-forums.org.

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    I guess I am not following what your asking I am sorry the code has worked until I added this new step and it has all been tested up to this point I guess the easiest way to explain what I need to do is show smaller bits of code.
    private double computeShippingCharges() {
            double shipping = 0;
            double total =0;
            double highest = 0;
            double lowest = 0;
     
     
            System.out.println("\nWelcome to the Shipping Wizard!\n");
            System.out.println("\nEnter 0 to end!\n");
     
             while (true) {
            // Acquire a price. A price == 0 indicates end-of-data
     
            System.out.print("Please enter shipping charges: $");
            double charges = scanner.nextDouble();
            shipping = charges + shipping;
            total = charges;
     
     
            // Test for end-of-data
             if (charges <= -1) {
             System.out.println("Please enter a positive number!");
             charges = scanner.nextDouble();
             } else if (charges > 1000) {
             System.out.println("That is unrealistic please enter the correct value!");
             charges = scanner.nextDouble();
             } else if (charges == 0){
             break;
             }
             }
     
     
            return shipping;
        }
    Basically I need to display the three outputs in the public WebHostOrder14(), the code in that section is this.
    shipping = computeShippingCharges();
            System.out.printf("\nThe shipping charges total up to $" + shipping);
            System.out.printf("\nThe highest shipping charge inputed was: $" + highest);
            System.out.print("\nThe lowest shipping charge inputes was: $" + lowest);

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    You skipped answering the questions I asked in post#6. Can you look at them and answer them?

    I'll ask them 0ne at a time if three are too many:
    What variables hold the values you want to print in the computeShippingCharges() method?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    shipping
    highest
    lowest

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    What class are those variables defined in?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    WebHost
    code is posted in post 1

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    Where is the variable: shipping defined in that class?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    Ok I see the confusion now because shipping is defined in public class WebHostOrder14 as well as highest and lowest, however highest and lowest is also defined in the overloaded constructor in WebHost. I do not know if this is where my problem is or not!
    Code for WebHostOrder 14 is;
    public class WebHostOrder14 {
     
        private double highestCharge; // instance variable for highestCharge
        private double lowestCharge; // instance variable for lowestCherge
        private Scanner scanner;
     
        //create a new constructor to do the work
        public WebHostOrder14() {
            WebHost webHost;
            double average;
            double shipping;
            double highest;
            double lowest;
     
            System.out.println("\nWelcome to the Web Host Ordering System\n");
            scanner = new Scanner(System.in);
     
            average = computeAverageSales();
            System.out.printf("\nThe average sales for these packages is: $" + average);
            if (average >= 200) {
                System.out.println("\nWow! Those sales are awesome!!\n");
            }
     
            shipping = computeShippingCharges();
            System.out.printf("\nThe shipping charges total up to $" + shipping);
            System.out.printf("\nThe highest shipping charge inputed was: $" + highest);
            System.out.print("\nThe lowest shipping charge inputes was: $" + lowest);
            ///LOOK You must display highest and lowest charges HERE...
        }

    Is any of the above code correct?

    I appreciate your patience with me!

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    Ok, forget about the shipping variable for now. If the two variables you want: highest and lowest are defined in the WebHost class, we'll move on to the third question:

    Does the class that the computeShippingCharges() method is in (WebHostOrder14) have a reference to an instance of the class (WebHost) that contains those variables?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    If I am not mistaking WebHost webHost; is what you are asking about right?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    This code creates an instance of the WebHost class:
     WebHost webHost = new WebHost();
    The variable: webHost refers to the instance of the class that was created by the new statement.

    Is there a reference to an instance of the WebHost class in the WebHostOrder14 class?
    Methods in the WebHostOrder14 class must have a reference to the WebHost class to access its methods and variables.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    I guess not, I just see the one I sent you in it if it is not to much trouble could you look over the WebHostOrder14 class and correct me if I'm wrong?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    These very basic concepts seem to be past your current knowledge. I suggest you work with a small program to learn how to create instances of classes and access their methods and variables. Work with something small and simple until you learn how things work, before trying to work with this much larger program.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Instance variable inside a method

    I have got it to work for the most part however I now get this as my output! Any suggestions on why my loop does not continue to run?
    run:

    Welcome to the Web Host Ordering System

    Please enter the price of the new package: $0

    The average sales for these packages is: $NaN
    Welcome to the Shipping Wizard!


    Enter 0 to end!

    Please enter shipping charges: $1

    Welcome to the Shipping Wizard!


    Enter 0 to end!

    Please enter shipping charges: $3

    Welcome to the Shipping Wizard!


    Enter 0 to end!

    Please enter shipping charges: $2

    The shipping charges total up to $1.0
    The highest shipping charge inputed was: $3.0
    The lowest shipping charge inputes was: $2.0

    Here is the code for the computeShippingCharges()
        private double computeShippingCharges() {
            double shipping = 0;
            double highest = 0;
            double lowest = 0;
     
     
            System.out.println("\nWelcome to the Shipping Wizard!\n");
            System.out.println("\nEnter 0 to end!\n");
     
             while (true) {
            // Acquire a price. A price == 0 indicates end-of-data
     
            System.out.print("Please enter shipping charges: $");
            double charges = scanner.nextDouble();
            shipping = charges + shipping;
            if (charges < lowest){
                charges = lowest;
            } else if (charges > highest){
                charges = highest;
            }
            // Test for end-of-data
             if (charges <= -1) {
             System.out.println("Please enter a positive number!");
             charges = scanner.nextDouble();
             } else if (charges > 1000) {
             System.out.println("That is unrealistic please enter the correct value!");
             charges = scanner.nextDouble();
             } else if (charges == 0){
             break;
             }
             }
            return shipping;
        }

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Instance variable inside a method

    why my loop does not continue to run?
    The break; statement will cause execution to exit the loop.
    What is the value of charges? Add a println statement and print out its value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Instance Variable does't work in extended class?
    By jean28 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2013, 01:36 AM
  2. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  3. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  4. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  5. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM