wood burning stoves*
The moose likes Beginning Java and the fly likes See if you can tackle this one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "See if you can tackle this one" Watch "See if you can tackle this one" New topic
Author

See if you can tackle this one

darrell clark
Greenhorn

Joined: Dec 07, 2006
Posts: 3
I have been given a second part to an assignement that wants me to create a program for comparing student obejects. The assignment description and code are below. I can' t see how the find to method interlocks with the findsmallest because find smallest is already finding the smallest value. I also don't see where the new diffinitions for UMUC_Comparable goes.

In the second part of this project, you will also find the smallest element in the array, but now the array will be of UMUC_Comparable objects. It will use the compareTo method to compare objects to find the smallest one. This method will also have the name findSmallest, but will take an array of UMUC_Comparable objects. This findSmallest method will have a definition as follows:

UMUC_Comparable findSmallest(UMUC_Comparable[] array);

The findSmallest method will use the compareTo method in the UMUC_Comparable interface. You will be using it with the Student objects from module V, section III, so you do not have to rewrite the compareTo method; you can simply use the one defined in the Student object in module V.

For the second part of this project, you should:

Create a main that creates an array of Student objects, as in section III of module V. You can use the same array as defined module V. You do not have to read these objects in from a file.
Call the findSmallest method to find the smallest Student object.
Use the getName and getAverage methods in the Student object to print out the smallest object.
Note that the return from the method is a UMUC_Comparable object, not a Student object, so you must cast the returned object to a Student object before printing it out. You can do so as follows:

Student[] students ....; // Fill in the declaration // of the student array.
Student s = (Student)findSmallest(UMUC_Comparable[] array);



/* File: Student.java
* Author: Darrell Clark
* Date: December 3, 2006
* Purpose: Shows how to find the smallest Int value in an array
*/
import java.util.*;
import java.io.*;

public class Student {
private int average;
private String name;
/* public constructor. Note that because no default
* constructor exists, Student objects will always
* be constructed with a name and an average.
*/
public Student(String name, int average) {
this.average = average;
this.name = name;
} // end method
/*
* return name
*/
public String getName() {
return name;
} // end method
/*
* return average
*/
public int getAverage() {
return average;
} // end method
/*
* compare to method for locating smallest value
*/
public static int findSmallest(int[] array) {
int min = Integer.MAX_VALUE;
for (int i = 1; i < (array.length); i++) {
if (array < min)
min = array;

}
return min;

}
/*
* compare student value
*/
public int compareTo(Student student) {
return (this.average - student.average);
} // end method

public static void main(String[] args) {
Student[] studentArray = { new Student("Tom", 87),
new Student("Cindy", 100),
new Student("Pat", 75),
new Student("Anne", 92),
new Student("Matt", 82)};
for (int i = 0; i < studentArray.length; i++) {
System.out.println(studentArray.name + " " +
studentArray.average);
} // end for
} // end method
} // end class
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Please reserve this forum for advanced questions.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

What is your question?
darrell clark
Greenhorn

Joined: Dec 07, 2006
Posts: 3
I just need a little insight as to how to approach this. I have some of the code i need, but i really am confused as to what the assignment wants me to create.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[darrell]: I can' t see how the find to method interlocks with the findsmallest because find smallest is already finding the smallest value. I also don't see where the new diffinitions for UMUC_Comparable goes.

I see you talking about a find method and findsmallest, but in the assignment description I only see findSmallest. So I'm not sure what you're asking. It sounds like for part 2 you make a second findSmallest method. The first was declared with

while the second

So your second method would be similar to the first, but, well, different in some ways. The idea is to start by making the change they just told you about (changing int it UMUC_Comparable), and try to figure out what else would have to change.

Now the bigger problem is that it sounds like UMUC_Comparable is probably supposed to be something they've given you some code for, somewhere. They talk about it here as if you're supposed to know about it already. Perhaps it was discussed in an earlier assignment? I'd check through your earlier notes.


"I'm not back." - Bill Harding, Twister
darrell clark
Greenhorn

Joined: Dec 07, 2006
Posts: 3
Really i am just concerned with making the code do what it was intended for. to used the methods i created to find the lowest array value of the student scores and print the students name and average.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: See if you can tackle this one