Extra credit I'm having some difficulty with

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1163 Views - Last Post: 13 December 2008 - 08:45 PM Rate Topic: -----

#1 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Extra credit I'm having some difficulty with

Posted 11 December 2008 - 03:25 PM

I'm always honest with you guys. So yes this is school related, however I don't have to do it, its just for some extra credit (which I don't need) but I want to learn it. This is what I have. I'm not sure if I'm doing it either correct, or appropriately. The code I do have doesn't work either, lol so go figure.

Instructions:
Write a program that will prompt the user for a list of 5 prices. Once the user has entered all values, your program should compute and display the following:

* The sum of all the prices
* The average of the prices
* All prices that are higher than the calculated average

To better solve this problem, break your code out into the following methods:

* sumArray � this method should receive an array and return the sum of all elements in the array. NOTE: this method produces no output.
* aveArray � this method should receive an array and return the average of all elements in the array. NOTE: this method produces no output.
* highPrices � this method should receive an array and an average. It should then print out all elements in the array whose values are greater than the average.


What I've been playing around with:
import java.util.*;
public class Prices{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [] someArray = read();
System.out.println("The sum of these values is " + sum(someArray));
System.out.print("Please enter a valu: ");
int value = sc.nextInt();
}
}

// it reads the elements of an array from the user
private static int [] read(){
Scanner sc = new Scanner(System.in);
int [] A;
A = new int[5];
for (int i = 0; i < A.length; i++){
System.out.print("Please enter a value: ");
A[i] = sc.nextInt();
}
return A;
}

// it finds the sum of the elements in an array
private static int sum(int [] A){
int total = 0;
for (int item: A)
total += item;
return total;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Extra credit I'm having some difficulty with

#2 homemade-jam  Icon User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 03:27 PM

Probably should have gone in the Java forum...is there anything you want particularly...does the code not run or does it not do what you want?
Was This Post Helpful? 0
  • +
  • -

#3 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 04:13 PM

Oh, I thought I did put this in the Java forum. I'm sorry. Well, what I do have doesn't work. But i'm not even sure if I'm doing it correctly to begin with to obtain the desired outcome.
Was This Post Helpful? 0
  • +
  • -

#4 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 06:23 PM

Any help at all is appreciated. If you know of some code that may give me a better direction to go in or anything you want to post here, whatever.. I just feel stuck and need a new path to start following so i can try it and ask questions if need be.
Was This Post Helpful? 0
  • +
  • -

#5 codellama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-October 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 07:13 PM

View PostJynxRD, on 11 Dec, 2008 - 05:23 PM, said:

Any help at all is appreciated. If you know of some code that may give me a better direction to go in or anything you want to post here, whatever.. I just feel stuck and need a new path to start following so i can try it and ask questions if need be.


What errors are you getting? Where are you lost? I can see a few things just by glancing at your code.

you have the method header like this
private static int [] read(){


is this what you are looking for?
private static read (int [] A){



Also you have this line, what are you trying to accomplish here?
int [] someArray = read();


You also print out the sum without asking the user for the values. Trying writing down the steps you need to accomplish, and attack each problem individually before attempting to code large chunks of it.
Was This Post Helpful? 0
  • +
  • -

#6 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 07:46 PM

codellama,

You've hit some of the spots I was having issues with. This is why I believe I may be going about this the wrong way. I don't want to spend all my time trying to fix this monstrosity I've created if there is a better or easier way of doing it that would involve rewriting all this to begin with.
Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:13 PM

The only error That I see is that the methods are outside of the class move the closing class brace to the end like this:

import java.util.*;
public class Prices{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int [] someArray = read();
		System.out.println("The sum of these values is " + sum(someArray));
		System.out.print("Please enter a value1: ");
		int value = sc.nextInt();  //What is this used for?
	}
	
//	}  Not here
//	it reads the elements of an array from the user
	private static int [] read(){
		Scanner sc = new Scanner(System.in);
		int [] A;
		A = new int[5];
		for (int i = 0; i < A.length; i++){
			System.out.print("Please enter a value: ");
			A[i] = sc.nextInt();
		}
		return A;
	}

//	it finds the sum of the elements in an array
	private static int sum(int [] A){
		int total = 0;
		for (int item: A)
			total += item;
		return total;
	}
}//But here



Also I suggest indenting your code as I have done above, as this is standard and the code is alot easier to read and spot the brace errors.
Was This Post Helpful? 0
  • +
  • -

#8 JBabineau  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 68
  • Joined: 05-December 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:17 PM

JynxRD your on the right track you have one } in the wrong place and its the } after you close your main method.

after you fix that and move it to the end of your program it reads and sums correctly

also it makes it easier to read ones code with indenting like the following:
import java.util.*;
public class Prices{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int [] someArray = read();
		System.out.println("The sum of these values is " + sumArray(someArray));
		System.out.print("Please enter a valu: ");
		int value = sc.nextInt();
	}


	// it reads the elements of an array from the user
	private static int [] read(){
		Scanner sc = new Scanner(System.in);
		int [] A;
		A = new int[5];
		for (int i = 0; i < A.length; i++){
			System.out.print("Please enter a value: ");
			A[i] = sc.nextInt();
		}
		return A;
	}

	// it finds the sum of the elements in an array
	private static int sumArray(int [] A){
		int total = 0;
		for (int item: A)
			total += item;
		return total;
	}
}



---------------
Edited: was beaten by BigAnt *head goes down in defeat*

This post has been edited by JBabineau: 11 December 2008 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:19 PM

Removed

This post has been edited by JynxRD: 11 December 2008 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:32 PM

You should stay with the original code you had, with the change i said made, as the original worked the way it was, to get it to work with doubles just change the arrays to double, and the nextInt to nextDouble as well as the return type for the avergae method.

Now to calculate the average just divide the result of getSum by the size of the array.

For the higher, just provide the values for the parameters specifed, loop through the loop and if the value there is > the given val print out this value.
Was This Post Helpful? 0
  • +
  • -

#11 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:46 PM

BigAnt,

Ok, I'm trying that but its not working


import java.util.*;
public class Prices{
     public static void main(String[] args){
          Scanner sc = new Scanner(System.in);
          int [] someArray = read();
          System.out.println("The sum of these values is " + sum(someArray));
			 System.out.println("The average of these values is " + avg(someArray));
          int value = sc.nextInt();  //What is this used for?
     }
     
//   it reads the elements of an array from the user
     private static int [] read(){
          Scanner sc = new Scanner(System.in);
          int [] A;
          A = new int[5];
          for (int i = 0; i < A.length; i++){
               System.out.print("Please enter a value: ");
               A[i] = sc.nextInt();
          }
          return A;
     }

//   it finds the sum of the elements in an array
     private static int sum(int [] A){
          int total = 0;
          for (int item: A)
               total += item;
          return total;
     }


     private static int avg(int [] A){
          int total = 0;
          for (int item: A)
               total /= item;
          return total;
     }
}


Was This Post Helpful? 0
  • +
  • -

#12 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:53 PM

     private static int avg(int [] A){
          return sum(A)/A.length;  //Ave = sum / length
     }
}


Was This Post Helpful? 0
  • +
  • -

#13 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 08:58 PM

Here is what I get as soon as I run it and input the first value

Quote

Please enter a value: 1.23
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Prices.read(Prices.java:21)
at Prices.main(Prices.java:8)

Was This Post Helpful? 0
  • +
  • -

#14 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 09:06 PM

This is because you have array of type int and are getting int from the console. Change the arrays to hold double values, as well the the console input to nextDouble.

Note you will also have to make changes to the return types for the sum and ave methods to returns doubles, as well as to some of the temp variables you use.
Was This Post Helpful? 0
  • +
  • -

#15 JynxRD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-November 08

Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 09:15 PM

Ok I think I got that part working, working as in it runs. The outcome though is :

Please enter a value: 1.23
Please enter a value: 2.34
Please enter a value: 3.45
Please enter a value: 4.56
Please enter a value: 5.00
The sum of these values is 16.58
The average of these values is 3.316

And it should be :

The sum of these values is 16.58
The average of these values is: 3.32


I tried using :

import java.text.NumberFormat;

NumberFormat formatter = new DecimalFormat("0.00");

System.out.println("The average of these values is " + formatter.format(avg(someArray)));


But this gives :

Prices.java:8: cannot find symbol
symbol : class DecimalFormat
location: class Prices
NumberFormat formatter = new DecimalFormat("0.00");

This post has been edited by JynxRD: 11 December 2008 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2