Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Home work

  1. #1
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Home work

    Hi . I'm Yana. I have some problem with my home work. Here my code:
    Java Code:
    import java.util.Vector;
    
    public class Diver 
    {
    	private String firstName;
    	private String lastName;
    	private Vector scores;
    	
    	public Diver()
    	{
    		firstName = "";
    		lastName = "";
    		scores = new Vector();
    	}
    	
    	public Diver(String firstName, String lastName, double... scores)
    	{
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.scores = new Vector();
    		
    		for (double score : scores)
    		{
    			this.scores.add( score );
    		}
    	}
    	
    	public String getFirstName() 
    	{
    		return firstName;
    	}
    
    	public void setFirstName(String firstName) 
    	{
    		this.firstName = firstName;
    	}
    
    	public String getLastName() 
    	{
    		return lastName;
    	}
    
    	public void setLastName(String lastName) 
    	{
    		this.lastName = lastName;
    	}
    	
    	public String toString()
    	{
    		return firstName + " " + lastName + scores.toString();
    	}
    }
    :confused:

    Java Code:
    
    import java.io.*;
    import java.util.*;
    
    class TestDiver
    {
    	public static void main(String[] args)
    	{
    		try {
    			Scanner scanner = new Scanner(new File("data.txt"));
    			scanner.useLocale(Locale.US);
    					
    			double[] scores = new double[8];
    
    			while (scanner.hasNext())
    			{
    				String firstName = scanner.next();
    				String lastName  = scanner.next();				
    				
    				double min = Double.MIN_VALUE;
    				double max = Double.MAX_VALUE;
    				
    
    				for (int i = 0; i < scores.length; i++)
    				{
    					scores[i] = scanner.nextInt();					
    				}
    				
    				Diver diver = new Diver(firstName, lastName, scores);				
    			}
    			scanner.close();
    		}
    		catch (Exception e)
    		{
    			System.out.println("Problem: " + e.getMessage());
    		}
    	}
    }
    :confused:
    Here data file:

    Richard Aston 9.2 9.3 9 9.9 9.5 9.5 9.6 9.8
    Emilie Hopkins 9.2 9.2 9 9.9 9.5 9.5 9.7 9.6
    Maria Ortega 9.2 9.2 9.1 9.9 9.5 9.6 9.4 9.8
    Franchesko Espinosa 9.2 9.3 9.2 9 9.5 9.3 9.6 9.8
    Paola Gonsales 9.2 9.3 9 9.4 9.1 9.5 9.6 9.8
    Melissa Wu 9.2 9.3 9.3 9.7 9.2 9.2 9.6 9.8
    Evelyn Eliston 9.2 9.2 9.2 9.9 9.5 9.2 9.3 9.8
    David Catcher 9.2 9 9.1 9.5 9.2 9.3 9.4 9.6
    William Richers 9.7 9.1 9.3 9.4 9.5 9.4 9.6 9.2

    I can't understant What's wrong?
    Thank you
    Write a TestDiverData class. This class will read the provided data file, create instances of Diver
    objects and populate them with the data as it's read from the data file. The TestDiverData class will
    store the created Diver objects in a Vector of it's own.
    Once all the data has been read and the Vector of Diver objects has been populated perform.For each Diver output the diver's name and total score using
    the provided scoring rules. Each contestant's score is calculated by dropping the lowest and highest
    scores and then adding the remaining scores. Format each diver's total score to two decimal places. So
    for example, the output for Richard Aston below would be: Richard Aston 56.90 points.
    Your program must read the data in from the provided data file and use arrays to store that data. Once
    all the data has been read in the program needs to calculate each diver's total points and output that
    diver's name and total points. Where total points is calculated based on the scoring rule defined above.
    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default

    What happens that makes you think something is wrong? Do you see an Exception? Some weird behavior? Something else?

    Read the link in my signature on asking smart questions, post an SSCCE that demonstrates the problem, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    So.
    What's wrong?
    You haven't actually told us...

  4. #4
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Output is:
    Problem: null

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

    Default

    Quote Originally Posted by yanakap View Post
    Output is:
    Problem: null
    In your catch block, do this:

    Java Code:
    e.printStackTrace();
    The stack trace will tell you exactly what happened and where it happened.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Diver.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    Tool completed successfully
    Output
    Problem: null

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

    Default

    Quote Originally Posted by yanakap View Post
    Diver.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    Tool completed successfully
    Output
    Problem: null
    That's not what I told you to do; reread my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    I tried
    System.out.println("e.printStackTrace():");
    e.printStackTrace();
    but It doesn't work

  9. #9
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Thank you for helping me.

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

    Default

    Quote Originally Posted by yanakap View Post
    I tried
    System.out.println("e.printStackTrace():");
    e.printStackTrace();
    but It doesn't work
    Only use this line:

    Java Code:
    e.printStackTrace();
    ... in your catch block; also reread one of my previous replies; copy&paste the entire stack trace here if you need more help.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    I did it but I didn't get output anything.

  12. #12
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Did you put it in the catch block?
    like this:
    Java Code:
    		catch (Exception e)
    		{
    			System.out.println("Problem: " + e.getMessage());
    			e.printStackTrace();
    		}
    Did you compile the code after you added the printStackTrace(); line?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    scores[i] = scanner.nextInt();
    You want to read doubles not ints.

  14. #14
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Yes I did e.printStackTrace(); and I did scores[i] = scanner.nextDouble();
    Nothing works (((((

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by yanakap View Post
    Nothing works
    That provides zero information, especially when it is incorrect. There are plenty of things that work.

    What does your program do now. If you have fixed your code by calling nextDouble then I suspect that your code is now not throwing any exceptions and you do not see anything because all your code does is read in a bunch of data, create a Diver object then loops, creates another Diver object, throws away the previous Diver object, continues to the end of the data file and exits the program. There is no other code such as printing output so you can see what it is doing.

  16. #16
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I copy&paste your code and I change as what members told you, and it works.

    Post your latest code so we can see what have you change...

  17. #17
    yanakap is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    import java.util.Vector;
    
    public class Diver
    {
    	private String firstName;
    	private String lastName;
    	private Vector scores;
    
    	public Diver()
    	{
    		firstName = "";
    		lastName = "";
    		scores = new Vector();
    	}
    
    	public Diver(String firstName, String lastName, double... scores)
    	{
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.scores = new Vector();
    
    		for (double score : scores)
    		{
    			this.scores.add( score );
    		}
    	}
    
    	public String getFirstName()
    	{
    		return firstName;
    	}
    
    	public void setFirstName(String firstName)
    	{
    		this.firstName = firstName;
    	}
    
    	public String getLastName()
    	{
    		return lastName;
    	}
    
    	public void setLastName(String lastName)
    	{
    		this.lastName = lastName;
    	}
    
    	public String toString()
    	{
    		return firstName + " " + lastName + scores.toString();
    	}
    }

    Java Code:
    import java.io.*;
    import java.util.*;
    
    class TestDiver
    {
    	public static void main(String[] args)
    	{
    		try {
    			Scanner scanner = new Scanner(new File("data.txt"));
    			scanner.useLocale(Locale.US);
    
    			double[] scores = new double[8];
    
    			while (scanner.hasNext())
    			{
    				String firstName = scanner.next();
    				String lastName  = scanner.next();
    
    				double min = Double.MIN_VALUE;
    				double max = Double.MAX_VALUE;
    
    
    				for (int i = 0; i < scores.length; i++)
    				{
    					scores[i] = scanner.nextDouble();
    				}
    
    				Diver diver = new Diver(firstName, lastName, scores);
    			}
    			scanner.close();
    		}
    		catch (Exception e)
    		{
    			System.out.println("Problem: " + e.getMessage());
    		}
    	}
    }
    and data.txt:


    Richard Aston 9.2 9.3 9 9.9 9.5 9.5 9.6 9.8
    Emilie Hopkins 9.2 9.2 9 9.9 9.5 9.5 9.7 9.6
    Maria Ortega 9.2 9.2 9.1 9.9 9.5 9.6 9.4 9.8
    Franchesko Espinosa 9.2 9.3 9.2 9 9.5 9.3 9.6 9.8
    Paola Gonsales 9.2 9.3 9 9.4 9.1 9.5 9.6 9.8
    Melissa Wu 9.2 9.3 9.3 9.7 9.2 9.2 9.6 9.8
    Evelyn Eliston 9.2 9.2 9.2 9.9 9.5 9.2 9.3 9.8
    David Catcher 9.2 9 9.1 9.5 9.2 9.3 9.4 9.6
    William Richers 9.7 9.1 9.3 9.4 9.5 9.4 9.6 9.2

  18. #18
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Your code is working. The only problem is you do not have code to output/print the result.

    try to put this code after String lastName = scanner.next();
    Java Code:
    System.out.println("Firstname: " + firstName);
    System.out.println("Lastname: " + lastName);

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by mine0926 View Post
    Your code is working. The only problem is you do not have code to output/print the result.
    Just like I said!

  20. #20
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

Page 1 of 2 12 LastLast

Similar Threads

  1. I want to work from home
    By silverglade in forum New To Java
    Replies: 15
    Last Post: 04-18-2011, 02:29 PM
  2. home work help
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 05:36 AM
  3. Home work help due midnight
    By alucard in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 04:09 AM
  4. Senior Java Desktop Developer Work From Home
    By carino in forum Jobs Offered
    Replies: 5
    Last Post: 08-11-2009, 02:45 PM

Posting Permissions

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