# calculate the percentage of missed hits and hits from the total

Page 1 of 1

## 12 Replies - 1308 Views - Last Post: 15 November 2011 - 09:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=255705&amp;s=ebd40242d1111db1b3734640f03c360a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 giraldo21

Reputation: 0
• 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
// Add numbers to the Player2 object
//--------------------------------------------------------------------------------------
// 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

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,400
• 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.

### #3 giraldo21

Reputation: 0
• 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

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,400
• 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.

### #5 giraldo21

Reputation: 0
• 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

### #6 Fuzzyness

• Comp Sci Student

Reputation: 669
• 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

### #7 giraldo21

Reputation: 0
• 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

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,400
• 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.

### #9 Fuzzyness

• Comp Sci Student

Reputation: 669
• 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.

### #10 giraldo21

Reputation: 0
• 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.

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,400
• 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.

### #12 Fuzzyness

• Comp Sci Student

Reputation: 669
• 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.

### #13 giraldo21

Reputation: 0
• 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