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 :
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 .
Re: Need Help : student Java programming Beginner
I'd define the following interface and a single implementation thereof:
Code:
interface Statistics {
void add(double n);
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
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.
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.
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.
Re: Need Help : student Java programming Beginner
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 );
}
}
Thank you for your interest
tried to implement this program, but he always gives me an error
I can not find it ,
Re: Need Help : student Java programming Beginner
1 Attachment(s)
Re: Need Help : student Java programming Beginner
Attachment 2409
When I finished writing the program, but there is an error I can not select it
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:
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.
1 Attachment(s)
Re: Need Help : student Java programming Beginner
I did what you said to me:
Code:
public class Chaos {
It seems that he will work correctly without this error
Attachment 2410
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. :)
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.
Re: Need Help : student Java programming Beginner
SAve your code as chaos.java
your code goes here which I have copied from your last post.
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.
Re: Need Help : student Java programming Beginner
Quote:
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).