# Extra credit I'm having some difficulty with

• (2 Pages)
• 1
• 2

## 28 Replies - 1652 Views - Last Post: 13 December 2008 - 08:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=76298&amp;s=080fa9cb72d9919371c22d5712b002cb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JynxRD

Reputation: 4
• 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);
System.out.println("The sum of these values is " + sum(someArray));
int value = sc.nextInt();
}
}

// it reads the elements of an array from the user
Scanner sc = new Scanner(System.in);
int [] A;
A = new int[5];
for (int i = 0; i < A.length; i++){
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;
}

```

Is This A Good Question/Topic? 0

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

• Gabe's Nemesis

Reputation: 11
• 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?

### #3 JynxRD

Reputation: 4
• 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.

### #4 JynxRD

Reputation: 4
• 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.

### #5 codellama

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

## Re: Extra credit I'm having some difficulty with

Posted 11 December 2008 - 07:13 PM

JynxRD, 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.

### #6 JynxRD

Reputation: 4
• 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.

### #7 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• 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);
System.out.println("The sum of these values is " + sum(someArray));
int value = sc.nextInt();  //What is this used for?
}

//	}  Not here
//	it reads the elements of an array from the user
Scanner sc = new Scanner(System.in);
int [] A;
A = new int[5];
for (int i = 0; i < A.length; i++){
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;
}
}//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.

### #8 JBabineau

Reputation: 6
• 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);
System.out.println("The sum of these values is " + sumArray(someArray));
int value = sc.nextInt();
}

// it reads the elements of an array from the user
Scanner sc = new Scanner(System.in);
int [] A;
A = new int[5];
for (int i = 0; i < A.length; i++){
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;
}
}

```

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

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

### #9 JynxRD

Reputation: 4
• 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

### #10 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• 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.

### #11 JynxRD

Reputation: 4
• 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);
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
Scanner sc = new Scanner(System.in);
int [] A;
A = new int[5];
for (int i = 0; i < A.length; i++){
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;
}

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

```

### #12 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• 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
}
}

```

### #13 JynxRD

Reputation: 4
• 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

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.main(Prices.java:8)

### #14 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• 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.

### #15 JynxRD

Reputation: 4
• 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 :

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