# Thread: Need Help : student Java programming Beginner

1. Member Join Date
Dec 2011
Posts
5
Rep Power
0

## Need Help : student Java programming Beginner

Hello all
I'm a new member
I am a student Java programming Beginner , and I need some help in writing a program I tried hard to write correctly, but failed to do that ,
I hope that I find help here

*The Question : (Counting positive and negative numbers and computing the average of numbers)
write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). your program ends with the input 0 . Display the average as a floating-point number.

here is a sample run :

PHP Code:
```Enter an int value, the program exits if the input is 0:
1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25```
Exactly what I needed
Steps to write the program in detail Step by step .  Reply With Quote

2. ## Re: Need Help : student Java programming Beginner

I'd define the following interface and a single implementation thereof:

Java Code:
```interface Statistics {
int getN();
double getTotal();
double getAverage();
}```
I'd instantiate two of those implementations, one for the positive numbers and one for the negative numbers; the rest is just an implementation detail.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Dec 2011
Posts
5
Rep Power
0

## Re: Need Help : student Java programming Beginner

Thanks for your help Mr. Jose
But I want the full details of the program in writing if you can.  Reply With Quote

4. ## Re: Need Help : student Java programming Beginner

That's not how this works. You try, we help. We do not write code for you. Give it a shot and come here with any trouble you run into.  Reply With Quote

5. ## Re: Need Help : student Java programming Beginner

If you need general help getting started, please have a look at these two links:

The Java Tutorials: the Really Big Index
So, You Need to Write a Program but Don't Know How to Start

Then please come on back if you have any more specific questions and we'll be more than happy to try to help you.  Reply With Quote

6. Member Join Date
Dec 2011
Posts
5
Rep Power
0

## Re: Need Help : student Java programming Beginner

PHP Code:
```package chaos;

import java.util.Scanner;

public class chaos {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.print("Enter an int value, the program exits if

the input is 0:  ");
int n = in.nextInt();

int positives  = 0,  negatives=0 ,  count = 0;
double sum=0 , ave=0;
for (; ;) {
sum+=n;
count++;

if (n == 0)
break;

if (n > 0) {

positives ++;
}
else{
negatives++;
}
ave= sum/count;

System.out.print("Enter an int value, the program exits if

the input is 0:");
n = in.nextInt();
}
System.out.println("The number of positives " +  positives

);

System.out.print("The number of negatives   " + negatives );

System.out.println (" \n The total is " + sum );

System.out.println (" The average is " + ave );

}
}```
tried to implement this program, but he always gives me an error
I can not find it ,
Last edited by wali; 12-26-2011 at 01:00 AM.  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Need Help : student Java programming Beginner

What is the error?  Reply With Quote

8. Member Join Date
Dec 2011
Posts
5
Rep Power
0

## Re: Need Help : student Java programming Beginner  Reply With Quote

9. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Need Help : student Java programming Beginner

You have defined the class chaos to be in the package chaos. EITHER remove the package statement.

OR Create a directory chaos and copy the java file into it. Then the command to compile will use the path to that file, and the command to run the program will use the full name of the class:

Java Code:
```C:\Users\HP>javac chaos\chaos.java
C:\Users\HP>java chaos.chaos```
-----

Standard Java coding conventions specify that you should start the class name (but not the package) with a capital letter: Chaos.  Reply With Quote

10. Member Join Date
Dec 2011
Posts
5
Rep Power
0

## Re: Need Help : student Java programming Beginner

I did what you said to me:

PHP Code:
`public class Chaos {`
It seems that he will work correctly without this error   Reply With Quote

11. ## Re: Need Help : student Java programming Beginner

CaPiTaLiZaTiOn matters! But seriously, your class is named Chaos and your file chaos. You should be able to figure out how to fix this. :)  Reply With Quote

12. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Need Help : student Java programming Beginner

I was trying to make two points:

(1) The class (and the file, but not the package) should be Chaos
(2) The Chaos.java file goes into a newly created directory chaos. And the command to compile it changes accordingly.  Reply With Quote

13. Senior Member Join Date
Apr 2010
Location
Posts
180
Rep Power
0

## Re: Need Help : student Java programming Beginner

your code goes here which I have copied from your last post.
Java Code:
```import java.util.Scanner;

public class chaos {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.print("Enter an int value, the program exits if

the input is 0:  ");
int n = in.nextInt();

int positives  = 0,  negatives=0 ,  count = 0;
double sum=0 , ave=0;
for (; ;) {
sum+=n;
count++;

if (n == 0)
break;

if (n > 0) {

positives ++;
}
else{
negatives++;
}
ave= sum/count;

System.out.print("Enter an int value, the program exits if

the input is 0:");
n = in.nextInt();
}
System.out.println("The number of positives " +  positives

);

System.out.print("The number of negatives   " + negatives );

System.out.println (" \n The total is " + sum );

System.out.println (" The average is " + ave );

}
}```
execute those commands for compiling and executing.I think you will be able to fix the problem
For you,
JAVA is a Case-sensitive language.  Reply With Quote

14. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Need Help : student Java programming Beginner

your code goes here which I have copied from your last post
Just so we're clear: UJJAL DHAR's version of the code is not in a package. And this will change both were the .java file should be placed (it should be in the directory from where you compile) and the commands you use to compile and run (they will not mention the package or its associated directory/folder).  Reply With Quote

#### Posting Permissions

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