aspose file tools*
The moose likes Beginning Java and the fly likes not allowing 2 of the same imputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "not allowing 2 of the same imputs" Watch "not allowing 2 of the same imputs" New topic
Author

not allowing 2 of the same imputs

Peggy Connor
Greenhorn

Joined: Apr 27, 2003
Posts: 1
Hi
I'm taking a java class and am stuck on our last assignment. We have a project called Assign7 that has 4 of our other completed assignments in it. 1 is called Gradeable class, the other is Grades, another is ExamGrades,and the last one is assign6, which is a box that you have to input the date, the description, another box with maximum point allowed and earned points. I made the box and buttons with the JButton and everything, but now the teacher says we have to add equals and a hashSet so that when a person says they got 98 on their last grade and then when they go to add another grade and they put in 98 again, (or any other duplicate grade for that matter), they get a silent error or a printed error saying that no two grades are allowed.
We are ripping our hair out. Can you help? this is my Gradeable class for which the teacher says we have to add a hashSet and equals too. I really appreciate any help.
PC
import java.io.Serializable;
import java.util.Date;
import java.text.DecimalFormat;
/**
* An Gradeable represents work submitted for a grade.
*
* @author
* @version 1.4.1
*/
public final class Gradeable implements Serializable {

/** Date on which this Gradeable is due. */
private final Date date;

/** Describes what the Gradeable represents: quiz, test, project, etc. */
private final String description;

/** Maximum possible points. */
private final int maxPoints;

/** Actual points earned. */
private final int earnedPoints;


/** Constructs a <code>Gradeable</code> using the specified parameters.
* @param date due date
* @param description description, must be non-null
* @param maxPoints maximum possible points, must be non-zero
* @param earnedPoints points earned, cannot be negative!
*
* @throws IllegalStateException if any parameters are invalid
*/
public Gradeable(final Date date, final String description, int maxPoints,
int earnedPoints) {
this.date = new Date(date.getTime()); // make defensive copy
this.description = description;
this.maxPoints = maxPoints;
this.earnedPoints = earnedPoints;
testInvariant();
}

/**
* Detects and rejects duplicate grades. This method overrides
* the Object equals method.
*
* @param Object o o is reference to an object
* @return testing if the fields of two objects have the identical values
*
*/
public boolean equals(Object o){
if (o == this){
return true;
}
if (o == null){
return false;
}
if (! (o instanceof Gradeable)){
return false;
}

Gradeable other = (Gradeable) o;
return maxPoints == other.maxPoints
&& earnedPoints == other.earnedPoints
&& description.equals(other.description)
&& date.equals(other.date);
}

/**
* This method overrides the Object hashCode method.
* @return the hash code for the Object
*/
public int hashCode() {
int result = 17;

result = 37 * result + maxPoints;
result = 37 * result + earnedPoints;
result = 37 * result + description.hashCode();
result = 37 * result + date.hashCode();
return result;
}

/** Calculates the percentage grade for this Gradeable.
*
* @return the calculated score as a fraction <= 1.0
*/
public double score() {
return (double) earnedPoints / maxPoints / 100.0;
}


/** Returns a formatted <code>Gradeable</code>.
* @return a string representation of the object; the representation is
* subject to change.
*/
public String toString() {
DecimalFormat df = new DecimalFormat("#,##0.00%");
return getDescription() + ", max points = " + getMaxPoints() +
", earned points = " + getEarnedPoints() + ", score = " +
df.format(score() * 100);
}


/** Returns the description.
* @returns the <code>Gradeable</code> description
*/
public String getDescription() {
return description;
}


/** Returns the maximum points possible.
* @returns the <code>Gradeable</code> maximum points
*/
public int getMaxPoints() {
return maxPoints;
}


/** Returns the earned points.
* @returns the <code>Gradeable</code> earned points
*/
public int getEarnedPoints() {
return earnedPoints;
}


/** Returns the date.
* @returns the <code>Gradeable</code> date
**/
public Date getDate() {
return new Date(date.getTime());
}

/** Validates invariants. An invariant is a condition that should
* always be true. If an invariant is
* ever <em>not</em> true, then something is seriously wrong and we should
* stop the program rather than allow computation to continue.
*
* @throws IllegalStateException if the date, description, maximum or earned
* points is invalid
*/
private void testInvariant() {
if (date == null || description == null || maxPoints <= 0 ||
earnedPoints < 0)
throw new IllegalStateException(
"The date, description, maximum or earned points is invalid:\n" +
toString());
}


/** Tests Gradeable.
* @param args command-line arguments
*/
public static void main(String args[]) {
final Date date = new Date();
Gradeable g = new Gradeable(date, "Abstraction project", 100, 66);
System.out.println("First gradeable: " + g);

g = new Gradeable(date, "Quiz #1", 50, 0);
System.out.println("Second gradeable: " + g);

g = new Gradeable(new Date(), "Final Exam", 300, 300);
System.out.println("Third gradeable: " + g);

Gradeable g1 = new Gradeable(new Date(), "hi", 100, 100);
Gradeable g2 = new Gradeable(new Date(), "hi", 100, 100);
System.out.println(g1.equals(g2)); //better be true or equals is wrong

}
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&7&&
this is the last assignment called Assign7
*****************************************************

public final class Assign7 extends JFrame {

/** Gradeable date. */
private JTextField date = new JTextField();

/** Gradeable description. */
private JTextField description = new JTextField();

/** Gradeable maximum possible points. */
private JTextField maxPoints = new JTextField();

/** Gradeable earned pointss. */
private JTextField earnedPoints = new JTextField();

/** Component in which the grades are displayed. */
private JTextArea output = new JTextArea();

/** Event to be fired when it's time to close the application. */
private final AWTEvent CLOSE = new WindowEvent(this,
WindowEvent.WINDOW_CLOSED);

/** Date input format. */
private static final DateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy");

/** Exam grades. */
private Grades exams = new ExamGrades("Exams", .20);

/**
* Draws the user interface and starts the windowing system.
*/
public Assign7() {
super("Grade Book");
JPanel top = new JPanel(true);
JPanel bottom = new JPanel(true);
top.setLayout(new BorderLayout());
bottom.setLayout(new BorderLayout());
top.setBorder(BorderFactory.createLineBorder(Color.black));
bottom.setBorder(BorderFactory.createLineBorder(Color.black));
getContentPane().add(top, BorderLayout.NORTH);
getContentPane().add(bottom, BorderLayout.CENTER);

JPanel labels = new JPanel(true);
labels.setLayout(new GridLayout(4, 1, 0, 0));
labels.add(new JLabel(" Date: ", SwingConstants.RIGHT));
labels.add(new JLabel(" Description: ", SwingConstants.RIGHT));
labels.add(new JLabel(" Max Points: ", SwingConstants.RIGHT));
labels.add(new JLabel(" Earned Points: ", SwingConstants.RIGHT));
top.add(labels, "West");

JPanel parameters = new JPanel(true);
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(date);
parameters.add(description);
parameters.add(maxPoints);
parameters.add(earnedPoints);
top.add(parameters, "Center");

JPanel buttons = new JPanel(true);
// Add buttons here ...
JButton reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
date.setText("");
description.setText("");
maxPoints.setText("");
earnedPoints.setText("");
}
});
buttons.add(reset);

JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (! isValidInt(maxPoints.getText()) ||
! isValidInt(earnedPoints.getText())) {
JOptionPane.showMessageDialog(null, "Points must be a number",
"Invalid input", JOptionPane.ERROR_MESSAGE);
return;
}
if (! isValidDate(date.getText())) {
JOptionPane.showMessageDialog(null, "Invalid date, use " +
"mm/dd/yyyy", "Invalid date", JOptionPane.ERROR_MESSAGE);
return;
}
exams.add(new Gradeable(new Date(),
description.getText(),
Integer.parseInt(maxPoints.getText()),
Integer.parseInt(earnedPoints.getText())));
}
});
buttons.add(add);

JButton view = new JButton("View");
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
output.setText(exams.toString());
}
});
buttons.add(view);

JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispatchEvent(CLOSE); // fires a window closed event
}
});

buttons.add(exit);
top.add(buttons, "South");
JScrollPane p = new JScrollPane(output);
bottom.add(p, "Center");
}

/**
* @param i int to be validated
* @return true if the specified int is valid
*/
private boolean isValidInt(final String i) {
try {
Integer.parseInt(i);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* @param d date to be validated
* @return true if the specified date is valid
*/
private boolean isValidDate(final String d) {
try {
dateFormat.parse(d);
return true;
} catch (ParseException e) {
return false;
}
}

/** Execution begins here.
* @param args command-line arguments
*/
public static void main(String[] args) {
JFrame f = new Assign7();
f.setBounds(100, 100, 300, 300);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Maybe you can contact Pamela Jones she's got (or had) a very similar problem.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
wood burning stoves
 
subject: not allowing 2 of the same imputs