Results 1 to 4 of 4
  1. #1
    mstratmann is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Help with instance variables in 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!
    Java Code:
    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
    Java Code:
    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
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with instance variables in a method

    Also at javaprogrammingforums.com.

    @OP: If you start a discussion in multiple places please post links in each to the others. That way people taking part in the discussion know what else is being said.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Help with instance variables in a method

    Moved to New To Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with instance variables in a method

    Quote Originally Posted by mstratmann View Post
    I am working on a project where I need to return 3 values I understand I can only return one using a method alone!
    True, but that single return value can be an object of an arbitrary class that contains three values; create that little class and return an instance of it and return that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Instance variables
    By jwl in forum New To Java
    Replies: 2
    Last Post: 10-24-2012, 11:29 PM
  2. Instance Variables
    By TriSBR in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 04:16 PM
  3. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 06:30 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
  •