12 Replies - 437 Views - Last Post: 15 November 2011 - 09:23 AM Rate Topic: -----

#1 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

calculate the percentage of missed hits and hits from the total

Posted 14 November 2011 - 08:26 PM

I want to calculate the percentage of missed hits and hits from the total using a linked list I was previous using and arraylist but i want to change to a linked list . How can i do that

greetings

this is the array that i used to do that:
//create an ArrayList object
        ArrayList <String> Player1 = new ArrayList();    //Player1 arrayList
        ArrayList <String> Player2 = new ArrayList();    //Player2 arrayList
        //--------------------------------------------------------------------------------------
        // Add numbers to the Player1 object
        Player1.add("0");    //arrayList location 0
        Player1.add("0");    //arrayList location 1
        Player1.add("1");    //arrayList location 2
        Player1.add("1");    //arrayList location 3
        Player1.add("1");    //arrayList location 4
        Player1.add("0");    //arrayList location 5
        Player1.add("0");    //arrayList location 6
        Player1.add("1");    //arrayList location 7
        Player1.add("1");    //arrayList location 8
        Player1.add("1");    //arrayList location 9
        // Add numbers to the Player2 object
        Player2.add("0");    //arrayList location 0
        Player2.add("1");    //arrayList location 1
        Player2.add("0");    //arrayList location 2
        Player2.add("0");    //arrayList location 3
        Player2.add("1");    //arrayList location 4
        Player2.add("0");    //arrayList location 5
        Player2.add("1");    //arrayList location 6
        Player2.add("0");    //arrayList location 7
        Player2.add("0");    //arrayList location 8
        Player2.add("1");    //arrayList location 9
        //--------------------------------------------------------------------------------------
        // add all the elemets in the array
        int sumPlayer1=0;
        int sumPlayer2=0;
        
        // sum all items to  corresponding array
        for (int x=0; x < (Player1.size()); x++){     
            sumPlayer1=sumPlayer1 + Integer.parseInt(Player1.get(x));  //Sum of all items in the first array            
        }
        // sum all items to  corresponding array
        for (int x=0; x < (Player2.size()); x++){                 
            sumPlayer2=sumPlayer2 + Integer.parseInt(Player2.get(x));  //Dum of all items in the second array
        }
        //--------------------------------------------------------------------------------------
        //define the sum of both arraylists
        int total=0;        
        total=sumPlayer1+sumPlayer2;  //Total sum of both arrays
        
        //calculate the percentage of hits from the total
        double totalHits;        
        totalHits = ((double)sumPlayer1 / total)*100;
        
        //calculate the percentage of missed hits from the total
        double totalMiss;        
        totalMiss = ((double)sumPlayer2 / total)*100;        
        //--------------------------------------------------------------------------------------
        //Displays all the hit/miss info to screen
        System.out.println("The percentage of hit is = " + (totalHits) + "%");
        System.out.println("The percentage of missed hits is = " + (totalMiss) + "%");
        System.out.println(sumPlayer1);
        System.out.println("The percentage of hit is = " + (totalHits) + "%");
        System.out.println("The percentage of missed hits is = " + (totalMiss) + "%");
        System.out.println(sumPlayer2);
    }


This post has been edited by macosxnerd101: 14 November 2011 - 10:37 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: calculate the percentage of missed hits and hits from the total

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,635
  • Joined: 27-December 08

Re: calculate the percentage of missed hits and hits from the total

Posted 14 November 2011 - 10:37 PM

Rather than writing ArrayList, write LinkedList. The ArrayList and LinkedList classes will produce the same results; they just go about it differently internally.
Was This Post Helpful? 0
  • +
  • -

#3 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 08:36 AM

do you have any example to illustrate that. i'm stuck building that code.

thank you once again
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,635
  • Joined: 27-December 08

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 08:41 AM

Here ArrayList <String> Player1 = new ArrayList();, you replace the text Array with the text Linked. That is it.
Was This Post Helpful? 0
  • +
  • -

#5 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 08:50 AM

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 08:55 AM

ArrayList <String> Player1 = new ArrayList();
Integer.parseInt(Player2.get(x));
If you are just going to make it an int why not make the arraylist of int instead of String? (well LinkedList now)

Also, you print out the same information twice:
System.out.println("The percentage of hit is = " + (totalHits) + "%");
        System.out.println("The percentage of missed hits is = " + (totalMiss) + "%");
        System.out.println(sumPlayer1);
        System.out.println("The percentage of hit is = " + (totalHits) + "%");
        System.out.println("The percentage of missed hits is = " + (totalMiss) + "%");


Are you trying to print out the total hits and miss % for each individual player?

This post has been edited by Fuzzyness: 15 November 2011 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#7 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:06 AM

yes Is king of statistic for a game (battleship) I want to know the score , the percentage of each player of hit and miss using a linked list. i was previously doing it in an array list but then my partner told me to change to a linked list
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,635
  • Joined: 27-December 08

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:07 AM

Why does your partner want you to use a LinkedList? It doesn't make sense to me to use a LinkedList here.
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:08 AM

Well you aren't getting each persons %'s. If you are going to do that you should take a look at this tutorial about Moving Away from Parallel Arrays. I would create a Class Player, and have methods in it to keep track of hits and then a method to get the total percentage and what not.
Was This Post Helpful? 1
  • +
  • -

#10 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:12 AM

well the thing is that we are using linked list now in class (group project) so he want to implement the game statistic in that way, for me the array list is perfect too you know. that why I was asking for help because linked list sound a little bit tricky for me.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,635
  • Joined: 27-December 08

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:16 AM

Writing your own Linked List might be a little trickier. Using the java.util.LinkedList class is very easy. That's the beauty of encapsulation. :)
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:16 AM

Generally if you have to Create an array list for each individual person you should re-reconsidering your design. If you make a Class Player, then create an ArrayList of Player, you can have methods in the Player class like attack(int) and if 0 increase the miss count in class, if a 1 increase hit count in class, then have method to get percentages for the player. In the driver class you can have a method like getTotalHits() which will get the total hits between both players etc.

Just realized you said battle ship, so you actually wont even need to create an LinkedList, just 2 Player Objects.
Was This Post Helpful? 1
  • +
  • -

#13 giraldo21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-November 11

Re: calculate the percentage of missed hits and hits from the total

Posted 15 November 2011 - 09:23 AM

ok thank you once again I have to see what i can do to fix that problem because class player has been done already so i have check how to implement it.

thank u again guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1