Results 1 to 14 of 14
- 12-25-2011, 01:34 PM #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 :
Exactly what I neededPHP 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
Steps to write the program in detail Step by step .
- 12-25-2011, 01:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Need Help : student Java programming Beginner
I'd define the following interface and a single implementation thereof:
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.Java Code:interface Statistics { void add(double n); int getN(); double getTotal(); double getAverage(); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-25-2011, 07:15 PM #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.
- 12-25-2011, 07:59 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 12-26-2011, 12:34 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
Re: Need Help : student Java programming Beginner
Thank you for your interestPHP 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.
- 12-26-2011, 12:37 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Need Help : student Java programming Beginner
What is the error?
- 12-26-2011, 01:07 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
- 12-26-2011, 04:58 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-26-2011, 09:57 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 5
- Rep Power
- 0
-
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. :)
- 12-27-2011, 02:05 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-28-2011, 09:26 AM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
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.
execute those commands for compiling and executing.I think you will be able to fix the problemJava 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 ); } }
For you,
JAVA is a Case-sensitive language.Don't Forget to try yourself before asking others help.....
Press REP, if you find their advices/solutions effective.
- 12-28-2011, 06:59 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Need Help : student Java programming Beginner
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).your code goes here which I have copied from your last post
Similar Threads
-
"Java Programming for the Absolute Beginner"
By finalstrike777 in forum New To JavaReplies: 2Last Post: 07-12-2011, 12:41 PM -
New Java Student in dire need of help
By Leonspade in forum New To JavaReplies: 11Last Post: 11-22-2010, 09:28 PM -
New java student, question about algorithm, please help :)
By siick in forum New To JavaReplies: 10Last Post: 08-27-2010, 03:31 AM -
A new student study Java
By MSs.Java in forum New To JavaReplies: 9Last Post: 04-05-2010, 01:23 PM -
New java student needs help
By cmizer in forum New To JavaReplies: 9Last Post: 12-13-2008, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks