Results 1 to 14 of 14
  1. #1
    wali is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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 .

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need Help : student Java programming Beginner

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

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    wali is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    wali is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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 );
    
    }
    }
    Thank you for your interest
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need Help : student Java programming Beginner

    What is the error?

  8. #8
    wali is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Need Help : student Java programming Beginner

    Need Help : student Java programming Beginner-jjjj.jpg

    When I finished writing the program, but there is an error I can not select it

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  10. #10
    wali is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default 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

    Need Help : student Java programming Beginner-ja.jpg

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  13. #13
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default 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.
    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.
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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).

Similar Threads

  1. A new student study Java
    By MSs.Java in forum New To Java
    Replies: 11
    Last Post: 06-20-2014, 08:31 AM
  2. "Java Programming for the Absolute Beginner"
    By finalstrike777 in forum New To Java
    Replies: 2
    Last Post: 07-12-2011, 12:41 PM
  3. New Java Student in dire need of help
    By Leonspade in forum New To Java
    Replies: 11
    Last Post: 11-22-2010, 09:28 PM
  4. Replies: 10
    Last Post: 08-27-2010, 03:31 AM
  5. New java student needs help
    By cmizer in forum New To Java
    Replies: 9
    Last Post: 12-13-2008, 06:16 AM

Posting Permissions

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