labs
This commit is contained in:
BIN
labs/lab7/IntList.class
Normal file
BIN
labs/lab7/IntList.class
Normal file
Binary file not shown.
50
labs/lab7/IntList.java
Normal file
50
labs/lab7/IntList.java
Normal file
@ -0,0 +1,50 @@
|
||||
// ****************************************************************
|
||||
// IntList.java
|
||||
//
|
||||
// An (unsorted) integer list class with a method to add an
|
||||
// integer to the list and a toString method that returns the contents
|
||||
// of the list with indices.
|
||||
// ****************************************************************
|
||||
public class IntList {
|
||||
protected int[] list;
|
||||
protected int numElements = 0;
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Constructor -- creates an integer list of a given size.
|
||||
//-------------------------------------------------------------
|
||||
public IntList(int size) {
|
||||
list = new int[size];
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Adds an integer to the list. If the list is full,
|
||||
// prints a message and does nothing.
|
||||
//-------------------------------------------------------------
|
||||
public void add(int value) {
|
||||
if (numElements == list.length) {
|
||||
System.out.println("Can't add, list is full");
|
||||
} else {
|
||||
list[numElements] = value;
|
||||
numElements++;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Returns the list elements with indices as a string.
|
||||
//-------------------------------------------------------------
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < numElements; i++) {
|
||||
result.append(i).append(": ").append(list[i]).append("\n");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Interface for sorting numbers.
|
||||
// -------------------------------------------------------------
|
||||
interface NumberSorter {
|
||||
void sortNumbers();
|
||||
}
|
BIN
labs/lab7/ListTest.class
Normal file
BIN
labs/lab7/ListTest.class
Normal file
Binary file not shown.
29
labs/lab7/ListTest.java
Normal file
29
labs/lab7/ListTest.java
Normal file
@ -0,0 +1,29 @@
|
||||
// ****************************************************************
|
||||
// ListTest.java
|
||||
//
|
||||
// A simple test program that creates an IntList, puts some
|
||||
// ints in it, and prints the list.
|
||||
//
|
||||
// ****************************************************************
|
||||
public class ListTest
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
IntList myList = new IntList(10);
|
||||
myList.add(100);
|
||||
myList.add(50);
|
||||
myList.add(200);
|
||||
myList.add(25);
|
||||
System.out.println(myList);
|
||||
|
||||
IntList intList = new IntList(10);
|
||||
intList.add(10);
|
||||
intList.add(5);
|
||||
intList.add(20);
|
||||
intList.add(15);
|
||||
|
||||
// Print the unsorted list
|
||||
System.out.println("IntList contents:");
|
||||
System.out.println(intList);
|
||||
}
|
||||
}
|
BIN
labs/lab7/NumberSorter.class
Normal file
BIN
labs/lab7/NumberSorter.class
Normal file
Binary file not shown.
43
labs/lab7/Player.java
Normal file
43
labs/lab7/Player.java
Normal file
@ -0,0 +1,43 @@
|
||||
// **********************************************************
|
||||
// Player.java
|
||||
//
|
||||
// Defines a Player class that holds information about an athlete.
|
||||
// **********************************************************
|
||||
import java.util.Scanner;
|
||||
public class Player
|
||||
{
|
||||
private String name;
|
||||
private String team;
|
||||
private int jerseyNumber;
|
||||
//-----------------------------------------------------------
|
||||
// Prompts for and reads in the player's name, team, and
|
||||
// jersey number.
|
||||
//-----------------------------------------------------------
|
||||
public void readPlayer()
|
||||
{
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.print("Name: ");
|
||||
name = scan.nextLine();
|
||||
System.out.print("Team: ");
|
||||
team = scan.nextLine();
|
||||
System.out.print("Jersey number: ");
|
||||
jerseyNumber = Scan.nextInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true; // Check if the two references are the same
|
||||
if (obj == null || getClass() != obj.getClass()) return false; // Ensure obj is a Player
|
||||
|
||||
Player other = (Player) obj; // Typecast obj to Player
|
||||
return team.equals(other.team) && jerseyNumber == other.jerseyNumber;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
// Returns a string representation of the player.
|
||||
//-----------------------------------------------------------
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Name: " + name + ", Team: " + team + ", Jersey Number: " + jerseyNumber;
|
||||
}
|
||||
}
|
BIN
labs/lab7/SortedIntList.class
Normal file
BIN
labs/lab7/SortedIntList.class
Normal file
Binary file not shown.
39
labs/lab7/SortedIntList.java
Normal file
39
labs/lab7/SortedIntList.java
Normal file
@ -0,0 +1,39 @@
|
||||
// ****************************************************************
|
||||
// SortedIntList.java
|
||||
//
|
||||
// A subclass of IntList that sorts the numbers in descending order.
|
||||
// ****************************************************************
|
||||
class SortedIntList extends IntList implements NumberSorter {
|
||||
//-------------------------------------------------------------
|
||||
// Constructor -- creates an integer list of a given size.
|
||||
//-------------------------------------------------------------
|
||||
public SortedIntList(int size) {
|
||||
super(size);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Sorts the numbers in descending order.
|
||||
//-------------------------------------------------------------
|
||||
@Override
|
||||
public void sortNumbers() {
|
||||
for (int i = 0; i < numElements - 1; i++) {
|
||||
for (int j = 0; j < numElements - i - 1; j++) {
|
||||
if (list[j] < list[j + 1]) {
|
||||
// Swap list[j] and list[j + 1]
|
||||
int temp = list[j];
|
||||
list[j] = list[j + 1];
|
||||
list[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Adds an integer to the list and sorts it.
|
||||
//-------------------------------------------------------------
|
||||
@Override
|
||||
public void add(int value) {
|
||||
super.add(value);
|
||||
sortNumbers();
|
||||
}
|
||||
}
|
BIN
labs/lab7/chapter8_lab-7.pdf
Normal file
BIN
labs/lab7/chapter8_lab-7.pdf
Normal file
Binary file not shown.
0
labs/lab7/chapter8_lab-7.pdfZone.Identifier
Normal file
0
labs/lab7/chapter8_lab-7.pdfZone.Identifier
Normal file
Reference in New Issue
Block a user