Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prices java (Array)

    I am relative new to java and it all seems to work 50% of the time ad the other 50% isa puzzel to me. I need some simple understanding. Please !!.My problem is : Create an application containing an array that stores 20 prices, such as $5.58,$3.48,$8.02,and so on. The application should (1) display the sum of all prices, (2)display all prices less than $3.00, (3) calculate the average of the prices,and (4) display all the values that are higher than the calcuated average value. I have a start but I am sure I got los along the way, lease I neen a helping hand .Thanking you in advance.

    public class Prices2 {
    public static void main(String[] args) {
    int size = (20);
    double prices[] = new double[size];
    System.out.println(
    "Please start entering the " + size +
    " prices one by one at the prompt(s).");

    for (int x = 0; x < size; x++);
    {
    System.out.println("What is price " + x + "? Enter it here: ");
    prices[x] = read double () ;

    System.out.println
    ("There are " + (size - x - 1) + " prices still to be entered.");
    }
    double largestThusFar = prices[0];
    double smallestThusFar = prices[0];
    double sumSoFar = 0;
    for (int x = 0; x < prices.length; x++) {
    if (largestThusFar < prices[x]) {
    largestThusFar = prices[x];
    }
    if (smallestThusFar > prices[x]) {
    smallestThusFar = prices[x];
    }
    sumSoFar = sumSoFar + prices[x];
    }
    double largest = largestThusFar;
    double smallest = smallestThusFar;
    System.out.println("The largest price is: " + largest);
    System.out.println("The smallest price is: " + smallest);
    double totalSum = sumSoFar;
    System.out.println("The average price is: " + totalSum / size);
    }
    }

  • #2
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Big Boss,

    I'm new to Java as well but I coded this for you. It's a bit more organized than your code so it might help you. Here it is:

    Code:
    import java.io.*;
    import java.util.*;
    public class Core {
    
    	public static void main(String[] args) throws IOException{
    		
    		double prices [] = new double[20];
    		double total;
    		int option;
    		Core c = new Core();
    		Scanner sc = new Scanner(System.in);
    		
    		do{
    			System.out.println("1. Enter prices");
    			System.out.println("2. Display sum of all prices");
    			System.out.println("3. Display prices lower than $3.00");
    			System.out.println("4. Display average ");
    			System.out.println("5. Display prices higher than calculated average");
    			System.out.println("6. Exit");
    			option = sc.nextInt();
    			
    			switch(option){
    			
    			case 1: 
    				c.addprices(prices);
    				break;
    			
    			case 2: 
    				c.showtotal();
    				break;
    				
    			case 3: 
    				c.showlower(prices);
    				break;
    				
    			case 4:
    				c.showaverage();
    				break;
    				
    			case 5:
    				c.showhigher(prices);
    				break;
    				
    			}
    			
    		}while(option != 6);
    		
    		}
    		
    		
    		
    	}
    
    
    /****************** Core Class**********************/
    
    import java.util.*;
    import java.io.*;
    
    public class Core {
    	
    			private double total = 0;
    			private double average = 0;
    					
    			public void addprices(double [] p){
    				
    				Scanner sc = new Scanner(System.in);	
    				int count = 20;
    				
    				
    				for(int i = 0; i < 20; i++){
    					
    					System.out.println("Add price (Remaining: "+count+"): ");
    					p [i] = sc.nextDouble();
    					count--;
    					total += p[i];
    				}
    					
    				}
    
    				
    			public void showtotal(){
    				System.out.println(total);
    				
    			}
    			
    			public void showlower(double [] p){
    				
    				for(int i = 0; i < 20; i++){
    					if(p[i] < 3)
    						System.out.println(p[i]);
    				}
    			}
    			public void showaverage(){
    				
    				average = total / 20;
    				System.out.println(average);
    			
    			}
    			
    			public void showhigher(double [] p){
    				
    			
    				for(int i = 0; i < 20; i++ ){
    					if(p[i] > average)
    						System.out.println(p[i]);
    				}
    			}
    }
    Last edited by Marshal; 11-05-2008 at 03:23 PM.

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by The Big Boss View Post
    Code:
        public class Prices2 {
           public static void main(String[] args) {  
             int size = (20); 
             double prices[] = new double[size];
             System.out.println(
                "Please start entering the " + size + 
                " prices one by one at the prompt(s)."); 
          
             for (int x = 0; x < size; x++);
             {
                System.out.println("What is price " + x + "? Enter it here: "); 
                prices[x] = read double () ;
                
                System.out.println
                   ("There are " + (size - x - 1) + " prices still to be entered."); 
             } 
    // ...
          } 
       }
    your for loop is not executing 20 times as you expect remove that offending semicolon at the end and it should work fine.
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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