By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,542 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,542 IT Pros & Developers. It's quick & easy.

Please can someone help me with this program

P: 3
Please can someone help me with this program. I am in a JAVA programming class and I am having a heck of a time. I am still having a problem with the basic concepts of JAVA but the teacher continues to give us difficult assignments with out any help. Here is the assignment:

Write a class named 'Player' that has two attributes, 'name' (which has to be a 'Sting') and 'rating' (which has to be a 'int'). Then write an application class named "PlayerRating" that first creates an array consisting of 5 player objects, then display the list three times; first unsorted, then sorted by Name and finally sorted by rating.

Must use the Bubble Sort Program and do NOT use Array.sort.

Example output:

Unsorted
Name: Rating:
Mickey Mantle 5
Ty Cobb 3
Babe Ruth 4
Pete Rose 3
Sammy Sosa 1

Sorted by Name
Name: Rating:
Babe Ruth 4
Mickey Mantle 5
Pete Rose 3
Sammy Sosa 1
Ty Cobb 3

Sorted by Rating
Name: Rating:
Sammy Sosa 1
Pete Rose 3
Ty Cobb 3
Babe Ruth 4
Mickey Mantle 5

Here is what I have and it is not much, cause I am so lost:
////////////////////////////////////////////////////////////////////////////////////
1 import java.io.*;
2 import javax.swing.*;
3 import java.text.*;
4 import java.lang.*;//String[][];
5 import java.util.*; //ArrayList;
6
7
8 public class Player
9 {
10 public static void main (String [][]args)
11 {
12
13 int maxSize = 100; //array size
14 // String[][] s = new String[100][100]; //Default values: null
15 boolean [] b = new boolean[4]; //default values: false
16 int[][] i = new int[10][10]; //default values: 0
17
18
19 PlayerRating arr; //reference to array
20 //arr = new PlayerRating(maxSize);
21 Sting player[]={"Mickey Mantle 5","Ty Cobb 3"
22 ,"Babe Ruth 4","Pete Rose 3","Sammy Sosa 1"};
23
24
25 }
26 }

************************************************** **
Here is all I have on the second program:
************************************************** **

public class PlayerRating
{
public void PlayerRating (int Max)
{
int[][] i = new int[10][10];
}

}
///////////////////////////////////////////////////////////////////////////////
Please help I am totally lost.
Nov 24 '06 #1
Share this Question
Share on Google+
2 Replies


P: 2
Expand|Select|Wrap|Line Numbers
  1. public class PlayerRating {
  2.  
  3.     Player[] players = new Player[] 
  4.         {
  5.             new Player("Mickey Mantle", 5),
  6.             new Player("Ty Cobb", 3),
  7.             new Player("Babe Ruth", 4),
  8.             new Player("Pete Rose", 3),
  9.             new Player("Sammy Sosa", 1)
  10.         };    
  11.  
  12.     class Player {
  13.         String name = null;
  14.         int rating = 0;
  15.         Player(String n, int r) {
  16.             name = n;
  17.             rating = r;
  18.         }
  19.         public String toString() {
  20.             return name + "    " + rating; 
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         PlayerRating pr = new PlayerRating();
  26.         System.out.println("\n\nUnsorted");
  27.         pr.playerDisplay();
  28.  
  29.         System.out.println("\n\nSorted by Name");
  30.         pr.bubbleSortName();
  31.         pr.playerDisplay();
  32.  
  33.         System.out.println("\n\nSorted by Rating");
  34.         pr.bubbleSortRating();
  35.         pr.playerDisplay();
  36.  
  37.     }
  38.  
  39.     void playerDisplay() {
  40.         System.out.println("Name   Rating");
  41.         for(int i=0; i<players.length; i++) {
  42.             System.out.println(players[i]);
  43.         }
  44.     }
  45.  
  46.     void bubbleSortRating() {
  47.         Player temp = null;    
  48.         int len = players.length;
  49.         for (int i = len - 1; i >= 0; i--) {
  50.             for (int j = 1; j <= i; j++) {
  51.                if (players[j-1].rating > players[j].rating) {
  52.                     temp = players[j-1];
  53.                     players[j-1] = players[j];
  54.                     players[j] = temp;
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     void bubbleSortName() {
  61.         Player temp = null;    
  62.         int len = players.length;
  63.         for (int i = len - 1; i >= 0; i--) {
  64.             for (int j = 1; j <= i; j++) {
  65.                String name_a = players[j-1].name;
  66.                String name_b = players[j].name;
  67.                int cmp = name_a.compareTo(name_b);
  68.                if (cmp >= 0) {
  69.                     temp = players[j-1];
  70.                     players[j-1] = players[j];
  71.                     players[j] = temp;
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
  77.  
you can check the bubble sort algo at this site http://linux.wku.edu/~lamonml/algor/sort/bubble.html
Nov 25 '06 #2

Ganon11
Expert 2.5K+
P: 3,652
You know, the man asked for help, not for you to write his program for him. That's called cheating in academic circles.

shblack, if you need some advice or guidance on where to go from this problem, please PM me, and I'd be glad to assist you.
Nov 26 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.