Results 1 to 10 of 10
  1. #1
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default New java student needs help

    hi guys thanks for reading this its abit long.

    I really need some help with my Java project. Its 20% of my final grade. Its pretty much beginner stuff but my teacher kinda sucks. Please if you are familiar with Java can you note me and help me? I`m not asking for someone who will just do it and give the code to me. I`m looking for someone who will explain it to me.

    Write a java program to:

    A. Repeatedly, do the following until the user indicates he doesn`t want to enter any more data:

    1. Read in from the keyboard a horse`s ID number (an integer between 20000 and 90000, inclusive)
    2. Read in the horse`s name.
    3. Read in the horses`s birthyear
    4. Read in the number of races the horse has been entered into. This will always be whole numbers
    5. Read in the number of races the horse has won, placed (come 2nd) and shown (3rd). These will always be whole numbers.
    6. Print out a percent analysis of the horse`s performance.
    7. Ask the user if he wants to enter info for another horse.

    When the user has finished enetering data, your program should print out a summary of the data entered.



    Ive already done the code for one horse.

  2. #2
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    What i got so far....i suck lol


    Java Code:
     import java.util.Scanner;
    import java.io.*;
    
    public class Horse4
    {
       public static void main (String[] args) throws IOException
       {
        
        Scanner sc=new Scanner(System.in);
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
       
        int horseId=0;
        String horsename=" ";
        int choice=0;
        int year=0;
        int race=0;
        int win=0;
        int place=0;
        int show=0;
        boolean i=false;
        boolean nor=false;    
        boolean nor2=false;    
        boolean nor3=false;    
        boolean o=false;
    
    
    
       // double winf=0.0;
        //double placef=0.0;
        //double showf=0.0;
    
        //int winp=0;
        //int placep=0;
        //int showp=0;
    
    
       do  
       {
         System.out.print("Please enter the horse ID number:\n");
        horseId=sc.nextInt();
    
        while (!i)
        {
    
        if (horseId>=20000&&horseId<=90000)    
        {
        i=true;
        }
        else
        {
          System.out.println("Invalid entry.Please enter the horse ID number:\n");
          horseId=sc.nextInt();
    
        }
       
    
      
     
        System.out.println("Please enter the horse`s name: ");
        horsename=br.readLine();
    
        System.out.println("Please enter " +horsename + "`s year of birth: ");
        year=sc.nextInt();
    
        System.out.println("Please enter the number of races " + horsename + " has entered: ");
        race=sc.nextInt();    
    
    
        System.out.println("Please enter the number of races " +horsename + " has won: "); 
        win=sc.nextInt();
    
        if (win>race)
        {
          nor=true;
          System.out.println("Invalid entry");
          win=sc.nextInt();
        }
        else
        {
    
        System.out.println("Please enter the number of races in which " +horsename + " has placed: ");
        place=sc.nextInt();
    
    
        if ((place>race)||(place+win>race))
        {
          nor2=true;
          System.out.println("Invalid entry");
          place=sc.nextInt();
      
        }
        else
        {
        System.out.println("Please enter the number of races in which " +horsename + " has shown: ");
        show=sc.nextInt();
    
    
    
        if ((show>race)||(place+win+show>race))
        {
         nor3=true;
         System.out.println("Invalid entry");
         show=sc.nextInt();
        }
        else
        {
        
    
        double winf =((double)(win)/race);
        double placef =((double)(place)/race);
        double showf = ((double)(show)/race) ;
    
        int winp = (int)(winf*100);
        int placep = (int)(placef*100);
        int showp = (int)(showf*100);
    
       System.out.println("\nHorse ID: " + horseId + "\nHorse name: " + horsename + "\nHorse birthday: " + year + "\nNumber of races entered: " + race +                       "\nNumber of times horse won: " + win +"\nNumber of times horse placed: " + place + "\nNumber of times horse shown: " + show );
       System.out.println("\n" + horsename + " has won " + winp + "% of his races, has placed " + placep + "% and has shown " + showp + "%.") ; 
       }
     
        System.out.println("again? Y =1 or N =2");
        choice=sc.nextInt();
        if (choice==1)
        {
        o=true;
        }
        else
        System.out.println("bye");
    
        }
    
       }
       
      
    
    while (o=true);
    
        }
    } 
    }
      }
      }
    }
    }
    }
    }
      }
    }
    }
       }
    }

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

    Default

    Why not try to do as much as you can here and then post your best effort code. That way we can work with your code (you'll be much better off this way) and we'll know exactly what you understand and what you don't.

    When you post code, please remember to use code tags ....
    [edit: never mind. while composing this you already posted with tags]

    Also, post any full error messages that you receive and indicate in the code with a comment any lines that throw these errors.

    Best of luck.

    [edit: why all the extra closing curly braces at the end of the posted file?]

  4. #4
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    i posted my code in my 2nd post :S

    oops just realised you saw it.

    I dont know i was trying new stuff n i guess i forgot to delete them and saved it.
    Last edited by cmizer; 12-13-2008 at 05:53 AM.

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

    Default

    Questions and Suggestions:

    Why use both a BufferedReader and a Scanner object? Combining them is just asking for trouble, and so I'd just use one, the simpler of the two -- the Scanner object.

    Why not break some of this down into separate methods? One method could be getHorseInfo(). Then your main method could be quite simple where you use a do / while loop and inside call getHorseInfo() followed by a System.out.print method that prompts the user to see if he wants to continue adding horses, and then retrieves his reply.

    You're going to potentially get several Horses of data here. How do you plan to store this information? The simplest would be to create a result String (or StringBuffer, but I'd stick with a String here as it's simpler and in this small example has no penalty compared with a StringBuffer) before the do-while loop and continually adding information to this String from within the loop. Then when done and finally out of the loop, print out this string using System.out.println(resultString). A more OOP like approach would be to create a Horse class, add all this information to newly created objects of this class, and then store this information in either an Array of some max size or better the more flexible ArrayList. I recommend the latter approach unless you've not created classes with constructors and such.

  6. #6
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    We havent learned arrays and stuff like that as yet. Using methods was the last topic we did at school and it wasn`t much. We have to do this the basic, hard way i`m guessing. If i use scanner class will i be able to input the horse`s name? Our teacher said it wont work. :S

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

    Default

    Yes, with Scanner you can enter the Horse's name. In fact Scanner can do it all here, read in ints, read in Strings, and read in lines. One warning about using Scanner though is that if you get individual tokens, such as reading in an int with the
    Java Code:
    int myInt = sc.nextInt();
    if you're at the end of a line, you may have to add an extra call to nextLine, like so:
    Java Code:
    int myInt = sc.nextInt();
    sc.nextLine(); // this "swallows" the end of line character

  8. #8
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    not that i`m not grateful for the help you are giving me here but do you think we can chat via IM? I cant post my email though, site restrictions, can you PM me yours?

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

    Default

    nah, I'm going to bed and I think that this goes against the spirit of the forum: sharing problems and solutions with all.

    In parting, my main recommendations are this:
    * Divide and conquer: don't try to write all the code at once. Instead solve each small problem, sometimes in its own small program file. Then when the little problem is solved, add it to the whole. The key here is to break down your big assignment into its small constituent tasks and solve each task in isolation.

    * Don't add good code to bad code. Only add small bits of code to your program at a time. After adding the code compile and run the program to make sure that there are no gross errors. Now most importantly, don't add more code to the program until the errors you have encountered here are fixed. Else you'll wind up with nothing but a bag of errors.

    Best of luck.

  10. #10
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    lol thanks for your help! Good night and sweet dreams. I`ll keep at it until i feel sleepy. Thanks again i`ll post my updates in the morning.

Similar Threads

  1. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 03:31 PM
  2. Student--begginer--help!!!
    By AmandaIT in forum New To Java
    Replies: 6
    Last Post: 05-09-2008, 03:37 AM
  3. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 05:31 PM
  4. help student
    By jvasilj1 in forum New To Java
    Replies: 15
    Last Post: 02-02-2008, 08:23 AM
  5. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 01:11 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
  •