Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I created this package, and the problem is when is runs it repeats the same catalog entries between bins.
I think the problem is in the detailedInventory method.
SHOULD BE: It should create a new random bin item (type, title, artist) for Bin B, not use the same element from Bin A. Other than this, the output is correct. SKU numbers are correctly generated and displayed.
**The values of i, when they correspond to the catalog, are objects. they are actually different types of physical media, such as DVDs or cassettes, with title, artist and SKU attributes.
***Although it does not seem to make sense, the data should be generated randomly. (This class is part of a larger concept/lesson to illustrate inheritance.)
Any help would be greatly appreciated!
Here is the output I see:
Bin A:
DVD - Greatest Hits Volume 2 (Limp Bizkit), SKU 1234-0: 500
Cassette - The Best Of (Michael Jackson), SKU 1234-1: 25
DVD - Love Songs (Michael Jackson), SKU 1234-2: 7720
Bin B:
DVD - Greatest Hits Volume 2 (Limp Bizkit), SKU 1234-3: 1000
The bin item under Bin B should not be the same as A.
public class testMusicMedia
{
public static ArrayList<MusicMedia> MakeMusicCatalog( int size )
{
String[] titles =
{
"Greatest Hits Volume 1", "Greatest Hits Volume 2",
"The Best Of", "Love Songs",
"The Early Years", "The Later Years"
};
String[] artists =
{
"Michael Jackson", "Eminem",
"The Beatles", "Shania Twain",
"Limp Bizkit"
};
ArrayList<MusicMedia> catalog = new ArrayList<MusicMedia>();
Random rn = new Random();
for ( int i = 0 ; i < size ; i++ )
{
MusicMedia m;
int mediatype = rn.nextInt( 3 );
String title = titles[ rn.nextInt( titles.length ) ];
String artist = artists[ rn.nextInt( artists.length ) ];
String sku = "1234-" + i;
if ( mediatype == 0 )
m = new CompactDisk( title, artist, sku );
else if ( mediatype == 1 )
m = new DigitalVideoDisk( title, artist, sku );
else
m = new CassetteTape( title, artist, sku );
catalog.add( m );
}
return catalog;
}
public static String lookupMedia( ArrayList<MusicMedia> catalog,
String sku )
{
for ( MusicMedia m : catalog )
{
if ( m.getSKU().equals( sku ))
return "SKU is in catalog";
}
return "SKU not in catalog";
}
public static String detailedInventory( ArrayList<MusicMedia> catalog, ArrayList<Bin> warehouse )
{
String s = "";
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for (int i = 0; i < bn.getContents().size(); i++){
s += catalog.get(i) + ", " + bn.getContents().get(i) + "\n";
}
}
s += "\n";
return s;
}
public static void main( String[] args )
{
ArrayList<MusicMedia> catalog = MakeMusicCatalog( 10 );
ArrayList<Bin> warehouse = new ArrayList<Bin>();
Bin a = new Bin( "A" );
Bin b = new Bin( "B" );
warehouse.add( a );
warehouse.add( b );
a.add( new BinItem( "1234-0", 500 ) );
a.add( new BinItem( "1234-1", 25 ) );
a.add( new BinItem( "1234-2", 7720 ) );
b.add( new BinItem( "1234-3", 1000 ) );
System.out.println( detailedInventory( catalog, warehouse ) );
}
}
I call the collection of all the items in a bin that share the same SKU a “bin item”. Each object of the Bin class (not shown) represents a bin in a pretend warehouse. A Bin object has two instance variables: a String that contains the bin name and an ArrayList that contains a BinItem for each of the SKUs stored in the bin. The toString method of the Bin class uses the toString method of the BinItem class. It generates lists that only involve SKUs and quantities.
This method should use a for-each loop to cycle thorugh all the bins in the warehouse. For each bin, s should be extended by the String "Bin " followed by the name of the bin, followed by a colon and \n to start new line. Then it should cycle through all the bin items in the current bin, for each one
extending s by a new line of text that begins with the result of looking up the current bin item’s SKU in the input catalog, and continue with a comma followed by the String representation of the bin item.
The output should look something like this:
Bin A:
CD - The Later Years (Shania Twain), SKU 1234-0: 500
Cassette - Greatest Hits Volume 2 (The Beatles), SKU 1234-1: 25
Cassette - Greatest Hits Volume 1 (Shania Twain), SKU 1234-2: 7720
Bin B:
Cassette - Greatest Hits Volume 2 (Michael Jackson), SKU 1234-3: 1000
Full code:
public class MusicMedia
{
private String myTitle,
myArtist,
mySKU;
public MusicMedia( String title, String artist, String sku )
{
myTitle = title;
myArtist = artist;
mySKU = sku;
}
public String getTitle()
{
return myTitle;
}
public String getArtist()
{
return myArtist;
}
public String getMediaType()
{
return "Unknown";
}
public String getSKU()
{
return mySKU;
}
public String toString()
{
return " - " + getTitle() + " (" + getArtist() + ")";
}
}
public class Disk extends MusicMedia
{
/**
* Constructor for objects of class Disk
*/
public Disk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "Disk";
}
public String toString()
{
return getMediaType() + super.toString();
}
}
I also have an identical CassetteTape class that also extends MusicMedia. Also two other subclasses of Disk, called CompactDisk and DigitalVideoDisk. These two are also almost identical to each other, so I have pasted the DVD class below.
public class DigitalVideoDisk extends Disk
{
/**
* Constructor for objects of class DigitalVideoDisk
*/
public DigitalVideoDisk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "DVD";
}
}
public class BinItem
{
private String mySKU;
private int myQuantity;
public BinItem( String sku, int quantity )
{
mySKU = sku;
myQuantity = quantity;
}
public String getSKU()
{
return mySKU;
}
public int getQuantity()
{
return myQuantity;
}
public String toString()
{
return "SKU " + getSKU() + ": " + getQuantity();
}
}
public class Bin
{
private String myName; //bin name
private ArrayList<BinItem> myContents; //contains a binItem
public Bin( String name )
{
myName = name;
myContents = new ArrayList<BinItem>();
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}
public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";
for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}
Ok, per your edit:
...the result of looking up the current bin item’s SKU in the input catalog...
You aren't doing this right now and it was the key point that we needed to understand what the program was supposed to do. Right now you're just retrieving an element from catalog using i, basically arbitrarily.
So the first thing you need to do is create a helper method that searches catalog for a MusicMedia object of a certain SKU. You have a method very much like it called lookupMedia so I've just modified it to this slightly different specification. This returns m instead of a String value:
public static MusicMedia getMediaBySKU(
ArrayList<MusicMedia> catalog, String sku
) {
for ( MusicMedia m : catalog ) {
if ( m.getSKU().equals(sku) )
return m;
}
return null;
}
Now that you are able to retrieve an item based on the SKU you can modify the detailedInventory loop to use it:
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for ( BinItem item : bn.getContents() ){
MusicMedia mm = getMediaBySKU(catalog, item.getSKU());
s += mm + ", " + item + "\n";
}
}
(Not sure how to get the SKU from the BinItem so I guessed but I hope you get the idea. If you don't have a method that returns the SKU already, you probably need to make one.)
Related
Hey so I will do my best at explaining this (and will have to change some code sorry if it looks weird) so I currently have:
public Job(String name, int tickets, double wage) {
this.name = name;
this.tickets = tickets;
this.wage = wage;
& and getters and setters for these and also making a list here:
Job Peter = new Job (name: "Peter", tickets: 100, wage: 1.55);
System.out.println(Peter);
So my issue is that lets say the total wage is calculated by tickets * wage how do I go about doing this by automation.
I can do this, however want a simpler method where it is automated so I can just for example Peter.getTotalWage
System.out.println(Peter.getTickets() * (Peter.getWage()));
My issue is when it comes to multiplying int with double, I've tried something like this but can't seem to get it working as I know it is wrong (probably completely wrong to be honest):
}
public double totalWage(int tickets, double wage) {
int i = tickets;
Double d2=Double.valueOf(i);
double sum = d2 * wage;
return sum;
}
Right now I have two classes.
If you can help us identify how to go about doing this that'll be great!
tl;dr
Use BigDecimal for money.
Organize your classes properly. Apparently you have:
An employee with a wage rate
A job whose cost is the number of tickets multiplied by the assigned employee's wage rate.
So you need an Employee class with a member variable for wage rate. And you need a Job class with an assigned Employee object, and a getWageCost method with this logic:
BigDecimal cost =
this.employee // An `Employee` object is assigned to this `Job` object.
.getWageRate() // The `Job` object asks the `Employee` object for its current wage rate.
.multiply(
new BigDecimal( this.countOfTickets )
)
.setScale( 2 , RoundingMode.HALF_EVEN ) // Round to the penny using Banker’s Rounding.
;
Tip to students: Notice how working towards a simple clear problem description written down in plain English can lead naturally to a proper class design. Lesson learned: Prose before code.
BigDecimal
For money, never use double or Double, float or Float. The floating-point types trade away accuracy for speed of execution. Use BigDecimal for fractional money.
BigDecimal wageRate = new BigDecimal( "1.55" ) ;
Multiply.
BigDecimal cost = this.employee.getWageRate().multiply( new BigDecimal( this.countOfTickets ) ) ;
But that line above fails to address the fractional penny (assuming this is United States dollars). So round to the penny or to the tenth or hundredth of a penny as directed by your accounting staff.
Round by calling the setScale method. Use the rounding method as directed by your accounting staff. Often in business matters that would be Banker's Rounding. So to the line above we should add a call to setScale with the number of digits (2 for whole pennies of US Dollar) and the typing of rounding.
BigDecimal cost = this.employee.getWageRate().multiply( new BigDecimal( this.countOfTickets ) ).setScale( 2 , RoundingMode.HALF_EVEN ) ;
The code above is logic on the Job class. Notice how it reaches into the Employee object to obtain the current wage rate for that worker.
Example code
Here is complete example code.
Employee alice = new Employee( "Alice" , "EMP00013" , new BigDecimal( "1.55" ) );
Job whateverJob = new Job( alice , 100 , "whatever" , UUID.randomUUID() );
BigDecimal wagesCostForWhateverJob = whateverJob.getWageCost();
System.out.println( "whateverJob: " + whateverJob + " costs " + wagesCostForWhateverJob );
whateverJob: Job{ employee=Employee{ name='Alice' | employeeId='EMP00013' | wageRate=1.55 } | countOfTickets=100 | description='whatever' | id=b4e397b3-e02e-42be-b117-622cee96a192 } costs 155.00
Employee.java
package work.basil.example;
import java.math.BigDecimal;
import java.util.Objects;
public class Employee
{
// ---------| Member vars |----------------------------------
private String name, employeeId;
private BigDecimal wageRate;
// ---------| Constructors |----------------------------------
public Employee ( String name , String employeeId , BigDecimal wageRate )
{
this.name = Objects.requireNonNull( name );
this.employeeId = Objects.requireNonNull( employeeId );
this.wageRate = Objects.requireNonNull( wageRate );
}
// ---------| Accessors |----------------------------------
public String getName ( )
{
return name;
}
public String getEmployeeId ( )
{
return employeeId;
}
public BigDecimal getWageRate ( )
{
return wageRate;
}
// ---------| Object |----------------------------------
#Override
public boolean equals ( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
Employee employee = ( Employee ) o;
return getEmployeeId().equals( employee.getEmployeeId() );
}
#Override
public int hashCode ( )
{
return Objects.hash( getEmployeeId() );
}
#Override
public String toString ( )
{
return "Employee{ " +
"name='" + name + '\'' +
" | employeeId='" + employeeId + '\'' +
" | wageRate=" + wageRate +
" }";
}
}
Job.java
package work.basil.example;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.UUID;
public class Job
{
// ---------| Member vars |----------------------------------
private Employee employee;
private Integer countOfTickets;
private String description;
private UUID id;
// ---------| Constructors |----------------------------------
public Job ( Employee employee , Integer countOfTickets , String description , UUID id )
{
this.employee = employee;
this.countOfTickets = countOfTickets;
this.description = description;
this.id = id;
}
// ---------| Accessors |----------------------------------
public Employee getEmployee ( )
{
return employee;
}
public Integer getCountOfTickets ( )
{
return countOfTickets;
}
public String getDescription ( )
{
return description;
}
public UUID getId ( )
{
return id;
}
public BigDecimal getWageCost ( )
{
BigDecimal cost = this.employee.getWageRate().multiply( new BigDecimal( this.countOfTickets ) ).setScale( 2 , RoundingMode.HALF_EVEN );
return cost;
}
// ---------| Object |----------------------------------
#Override
public boolean equals ( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
Job job = ( Job ) o;
return getId().equals( job.getId() );
}
#Override
public int hashCode ( )
{
return Objects.hash( getId() );
}
#Override
public String toString ( )
{
return "Job{ " +
"employee=" + employee +
" | countOfTickets=" + countOfTickets +
" | description='" + description + '\'' +
" | id=" + id +
" }";
}
}
In real work, if Job::getWageCost would be called very very often, we might consider caching the result. We could add a member variable to hold the result. But avoid falling into the trap of premature optimization.
I played around with the code a bit; maybe this helps?
public class Main {
public static void main(String[] args){
Job jobOne = new Job (100, 1.55);
Person p1 = new Person("Peter Williams");
double jobOnePay = jobOne.getTotal();
p1.setWage(jobOnePay);
System.out.println(p1.getName() + " gets paid: " + p1.getSetWage());
}
}
class Job{
private int tickets;
private double wage;
Job(int tickets, double wage) {
this.tickets = tickets;
this.wage = wage;
}
double getTotal(){
return tickets*wage;
}
}
class Person{
private String name;
private double setWage;
Person(String name){
this.name = name;
}
String getName() {
return name;
}
void setWage(double setWage) {
this.setWage = setWage;
}
double getSetWage() {
return setWage;
}
}
Output: "Peter Williams gets paid: 155.0"
I am tasked with creating 5 book objects and storing in an array, which I do no problem! I am able to print out the objects' variables using println no problem but when I want be able to move the variable around using printf I dont know how to call upon a specific variable of a certain element of the array. Please Help!
public class BookReport {
public static void main(String[] args){
Book[] TheBooks = new Book[5] ;
TheBooks[0] = new Book("Gone With The Wild", "Paul Mitchell", 1000) ;
TheBooks[1] = new Book("Harry Toilet", "Donald Trump", 100) ;
TheBooks[2] = new Book("Huckles Finn Berry", "SpiderMan", 500) ;
TheBooks[3] = new Book("The Bad Habbit", "Nose Picker", 700) ;
TheBooks[4] = new Book("Alien", "Mister Green", 600) ;
System.out.printf("%10s %20s %18s \n", "Book Title" , "Author", "Pages") ;
System.out.printf("%s \n", "----------------------------------------------------") ;
//This works but I cant justify the variables to align!!!
System.out.println(TheBooks[0]) ;
}
}
public class Book {
private String title = "" ;
private String author = "" ;
private int pages = 0 ;
public Book(String title, String author, int pages){
setTitle(title) ;
setAuthor(author) ;
setPages(pages) ;
}
public String getTitle(){
return title ;
}
public String getAuthor(){
return author ;
}
public int getPages(){
return pages ;
}
public void setTitle(String newTitle){
title = newTitle ;
}
public void setAuthor(String newAuthor){
author = newAuthor ;
}
public void setPages(int newPages){
pages = newPages ;
}
public String toString(){
return title + " " + author + " " + pages ;
}
public boolean equals(Book anotherBook){
return ((title.equals(anotherBook.title)) && (author.equals (anotherBook.author)) &&
(pages == anotherBook.pages)) ;
}
}
Just call the getters for the various fields you want to print:
System.out.printf("%10s %20s %18s \n", "Book Title" , "Author", "Pages");
System.out.printf("%s \n", "----------------------------------------------------") ;
for (Book b : TheBooks) {
System.out.printf("%10s %20s %18s \n", b.getTitle(), b.getAuthor(), b.getPages());
}
By the way, you named your array of books TheBooks, but it is Java convention to not name variables with capital letters as the first letter. So theBooks would work better here.
If you want to call specific variable of a certain element of the array.
you can use
java.util.Map
for example you can make Id for all object you want to store.
HashMap<String, Book> h = new HashMap<>();
Book book = new Book("Gone With The Wild", "Paul Mitchell", 1000);
h.put(book.getTitle(), book);
book = new Book("Harry Toilet", "Donald Trump", 100);
h.put(book.getTitle(), book);
book = new Book("Huckles Finn Berry", "SpiderMan", 500);
h.put(book.getTitle(), book);
book = new Book("The Bad Habbit", "Nose Picker", 700);
h.put(book.getTitle(), book);
book = new Book("Alien", "Mister Green", 600);
h.put(book.getTitle(), book);
// if you want to call it
Book b = h.get("exampletitle);
You should also be able to use String.format() in your toString method so that you have a reusable formatted version.
#Override
public String toString(){
return String.format("%10s %20s %18s \n", title, author, pages);
}
P.s. I think it would be good practice to include hashCode() for classes where you include an equals().
I'm having a little trouble with a fairly simple assignment, but couldn't find an answer here that seemed to work.
I need to have a 2D ArrayList that contains staff data, and then be able to run a separate function that allows the user to input a staff member's name, searches the ArrayList for that name, and if it exists, display the full row of data.
Here's what I've got so far:
The ArrayList
List<List<String>> staffArrayString = new ArrayList<>();
staffArrayString.add(Arrays.asList("Steven George", "12 York Road", "07123456678", "Permanent", "York", "27000/yr"));
staffArrayString.add(Arrays.asList("Rina Veyer", "20 Leeds Road", "08987987765", "Part Time", "Leeds", "10/hr"));
staffArrayString.add(Arrays.asList("Brian Lym", "13 Bradford Road", "07123234345", "Permanent", "Bradford", "27000/yr"));
The search function
public void staffNameSearch() {
System.out.println("Enter name of staff member:");
String staffName = in.next();
boolean found = false;
int row = 0;
for (int i = 0; i < staffArrayString.size(); i++) {
for (int j = 0; j < staffArrayString.get(i).size(); j++) {
if (staffArrayString.get(i).get(j).equals(staffName)) {
row = staffArrayString.get(i).size();
found = true;
}
}
}
if (found = true) {
System.out.print(staffArrayString.get(row) + " ");
}
}
I'm currently getting an output of 'Exception in thread "main" java.lang.IndexOutOfBoundsException' at the print line on the end there, but I can't for the life of me work out why. I'd appreciate any advice on this (especially if it's some obvious and stupid mistake on my part!).
The error is occuring because you are setting row to something unrelated to the row counter. When you discover the row (variable i) which has the name in the jth element, set row=i.
Be careful about if (found = true) - it is incorrect; prefer:
a) if (found)
b) if (found == true)
For efficiency, include && !found in the for loops so they exit as soon as you find something.
You can use for each loop for simpler.
System.out.println("Enter name of staff member:");
String staffName = in.next();
boolean found = false;
String[] foundArray;
for(String[] staffArray: staffArrayString){
for(String str : staffArray){
if(str.equals(staffName)){
foundArray = staffArray;
found = true;
break;
}
}
}
if (found == true) {
System.out.print(foundArray + " ");
}
You might be able to simplify the code a little bit by using a Map and a Staff class.
For example, the Staff class
public class Staff{
String name;
String address;
String id; // ?
boolean permanent; // Or a enum if there are more than 2 values
String city; // ?
int payrate;
boolean hourly;
#Override
public String toString(){ // Easily convert the class to a String
return String.format("%s %s %s %s %s $%d/%s",
name,
address,
id,
permanent ? "Permanent" : "Part-time",
city,
payrate,
hourly ? "hr" : "yr");
}
}
And for the code to read it
private Map<String, Staff> staffDirectory; // String is the name in lowercase
public void staffNameSearch() {
// Code
if(staffDirectory.containsKey(staffName)){ // Make sure staffName is lowercase
System.out.print(staffDirectory.get(staffName) + " ");
} else {
System.out.println("Name not found");
}
}
This way you can avoid using loops and get O(1) efficiency.
I'm trying to make a program in java that you can add people's birthdays, names, birthmonths, and birthyears. I'm having a hard time trying to come up with the code to remove an object from the arraylist here is the following code. How would I go about writing the removePerson method?
import java.util.ArrayList;
public class Analyzer {
// instance variables - replace the example below with your own
private final static int DAYS_PER_MONTH = 31;
private final static int MONTHS_PER_YEAR = 12;
private int[] birthDayStats;
private int[] birthMonthStats;
private ArrayList<Person> people;
/**
* Constructor for objects of class Analyzer
*/
public Analyzer() {
this.people = new ArrayList<Person>();
this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH];
this.birthMonthStats = new int[Analyzer.MONTHS_PER_YEAR];
}
public void addPerson(String name, int birthDay, int birthMonth, int
birthYear) {
Person person = new Person(name, birthDay, birthMonth, birthYear);
if (person.getBirthDay() != -1 || person.getBirthMonth() != -1) {
people.add(person);
birthMonthStats[birthMonth - 1]++;
birthDayStats[birthDay - 1]++;
} else {
System.out.println("Your current Birthday is " + birthDay + " or "
+ birthMonth + " which is not a correct number 1-31 or 1-12 please " +
"put in a correct number ");
}
}
public void printPeople() { //prints all people in form: “ Name: Tom Month: 5 Day: 2 Year: 1965”
int index = 0;
while (index < people.size()) {
Person person = (Person) people.get(index);
System.out.println(person);
index++;
}
}
public void printMonthList() { //prints the number of people born in each month
// Sample output to the right with days being similar
int index = 0;
while (index < birthMonthStats.length) {
System.out.println("Month number " + (index + 1) + " has " +
birthMonthStats[index] + " people");
index++;
}
}
public Person removePerson(String name) {// removes the person from the arrayList
}
}
/**
* Removes the {#code Person} with the given {#code name} from the list
* #param name the {#code Person}'s name
* #return the {#code Person} removed from the list or {#code null} if not found
*/
public Person removePerson(String name) {
if (name != null) {
for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) {
Person person = iter.next();
if (name.equalsIgnoreCase(person.getName())) {
iter.remove();
return person;
}
}
}
return null;
}
See the java.util.Iterator#remove() method.
Tuesday learning bonus:
If you want to look for a name faster in your list, you should consider to use a java.util.Map implementation:
HashMap<String,Person> people;
You can add Person objects in a smart way in order to make your search case insensitive:
people.put(name.toLowerCase(), new Person(name, ...));
... and your removePerson method become:
public Person removePerson(String name) {
if (name != null)
name = name.toLowerCase();
return people.remove(name);
}
See the java.util.Map#remove() method.
If you are using Java 1.8. It's pretty simple way to do. This will remove Person having 'name' from your list.
people.removeIf(x -> name.equalsIgnoreCase(x.getName()));
Right now I'm working on a method for comparing the scores of athletes in the olympics. So far I've had little trouble, however now I've reached a point where i need to compare two objects (athletes) scores and I'm not sure how to do it. This is my code for the Olympic class:
// A program using the Athlete class
public class Olympics {
public static void main(String args[]) {
System.out.println("The leader is " + Athlete.leader() +
", with a score of " + Athlete.leadingScore());
Athlete meryl = new Athlete("Meryl Davis", "U.S.");
meryl.addScore(75);
System.out.println(meryl);
Athlete tessa = new Athlete("Tessa Virtue", "Canada");
System.out.println(tessa);
System.out.println(); // blank line
tessa.addScore(50);
System.out.println(tessa);
System.out.println(meryl);
System.out.println("The leader is " + Athlete.leader() +
", with a score of " + Athlete.leadingScore());
System.out.println(); // blank line
tessa.addScore(100);
meryl.addScore(65);
System.out.println(tessa);
System.out.println(meryl);
System.out.println("The leader is " + Athlete.leader() +
", with a score of " + Athlete.leadingScore());
System.out.println(); // blank line
tessa.addScore(20);
System.out.println("Tessa's final score is " + tessa.getScore());
meryl.move("France");
System.out.println(meryl);
} // end main
} // end class Olympics
And this is the constructor class "Athlete":
public class Athlete {
private String name;
private String country;
protected int score;
public static int leadScore;
public Athlete(String athName, String athCountry) {
this.name = athName;
this.country = athCountry;
score = 0;
if (score < 1) {
System.out.println("Score cannot be lower than 1");
}
}
public int addScore(int athScore) {
score += athScore;
return score;
}
public static String leader(){
//TODO
}
public static int leadingScore() {
//MUST COMPARE BOTH ATHLETES
}
public int getScore(){
return score;
}
public void move(String newCountry) {
country = newCountry;
}
public String toString() {
return name + ": " + "from " + country + ", current score " + score;
}
}
So what I'm trying to do is have the program check Meryl's score compared to Tessa's and return that Athlete's score in leadingScore() and, using that athlete, return a leader(). Any help is appreciated! Thanks.
The function must take the two Athletes you're comparing as the parameters for this to work
public static int leadingScore(Athlete a1, Athlete a2) {
if (a1.getScore() < a2.getScore()) {
// do stuff
}
}
The lead score should not be in the athlete class, but rather in main () because one instance of an Athlete class would not know of other instances unless you put a self-referential list inside the class. Similarly, leadingScore should be in main ().
It or main can call each athlete and compare:
int merylScore = meryl.getScore ();
int tessaScore = tessa.getScore ();
int leadingScore = 0;
String leaderName = "";
if (merylScore > tessaScore) {
leadingScore = merylScore;
leaderName = meryl.getName ();
} else if (tessaScore > merylScore) {
leadingScore = tessaScore;
leaderName = tessa.getName ();
} else {
leadingScore = merylScore;
leaderName = "a tie between Meryl and Tessa";
}
System.out.println ("The leader is " + leaderName + ", with a score of " + leadingScore);
You should consider using a "collection". Use an array, a list ... or even a sorted list.
Stored your individual objects in the collection, then traverse the collection to find the highest score.
For example:
// Create athlete objects; add each to list
ArrayList<Athlete> athletes = new ArrayList<Athlete>();
Athlete meryl = new Athlete("Meryl Davis", "U.S.");
meryl.addScore(75);
...
athletes.add(meryl);
Athlete tessa = new Athlete("Tessa Virtue", "Canada");
...
athletes.add(tessa );
// Go through the list and find the high scorer
Athlete highScorer = ...;
for (Athlete a : athletes) {
if (highScorer.getScore() < a.getScore())
highScorer = a;
...
}
System.out.println("High score=" + highScorer.getScore());
Here's a good tutorial:
http://www.vogella.com/tutorials/JavaCollections/article.html