hi i need help with writing a program...I have to Write a program that simulates a student registration. A button allows entering a student name. Three more buttons are used to enter test scores. Name and test scores are entered into a text field. The information about the student, including student's name, score1, score2, score3, score average and the highest score are continully displayed. Design and write a class called Student. A Student object contains a student's name and three test scores and responds to the following messages.

Student() - a constructor that creates a student object. It initializes name to an empty string and sets all three test scores to 0.

setName(String aName) - sets the name of a student to aName.

getName() - returns the name of a student.

setScore(int whichTest, int testScore) - sets the score of whichTest(means test1 or test2 or test3) to testScore. Variable WhichTest indicates test number (test1, test2 or test3). Variable testScore indicates actual score (eg. 88, 76 etc.) on whichTest.

getScore(int whichTest) - returns the score on whichTest

getAverage() - returns the average of the test scores

getHighScore() - returns the highest test score.


The code below is what i have so far...i think it is right.. but nothing happens when i run the program. PLEASE HELP!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Student extends JFrame implements ActionListener {

// declare some GUI components
private String aName;
private String name;
private JPanel panel;
private JButton setName;
private JTextField textField;
private JButton setScoreOne;
private JButton setScoreTwo;
private JButton setScoreThree;
int scoreOne, scoreTwo, scoreThree;

public Student(String name, int gradeOne, int gradeTwo, int gradeThree){
name = "";
scoreOne = 0;
scoreTwo = 0;
scoreThree = 0;

}
public static void main(String[] args) {
Student demo = new Student();
demo.setSize(250, 400);
demo.setTitle("Test Scores");
demo.createGUI();
demo.setVisible(true);
}

private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE...
Container window = getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(150, 200));
panel.setBackground(Color.white);
window.add(panel);

//creates button that shows students score
setName = new JButton ("Set Name");
window.add(setName);
setName.addActionListener(this);

setScoreOne = new JButton("Set Test Score One");
window.add(setScoreOne);
setScoreOne.addActionListener(this);

setScoreTwo = new JButton ("Set Test Score Two");
window.add(setScoreTwo);
setScoreTwo.addActionListener(this);

setScoreThree = new JButton ("Set Test Score Three");
window.add(setScoreThree);
setScoreThree.addActionListener(this);

//creates size of the text field showing how many steps it took to leave town
textField = new JTextField(20);
window.add(textField);


}//end of createGUI

public void actionPerformed(ActionEvent event) {


}

public void setName (String aName){
//Set a student's name
name = aName;
}


public String getName (){
//Get a student's name
return name;
}


public void setScore (int whichTest, int score){
if (whichTest == 1) scoreOne = 86;
else if (whichTest == 2) scoreTwo = 90;
else scoreThree = 73;
}

public int getScore (int whichTest){
//Retrieve score for which test
if (whichTest == 1) return scoreOne ;
else if (whichTest == 2) return scoreTwo;
else return scoreThree;
}

public int getAverage(){
//Calculates and returns the average
int average;
average = (int) ((scoreOne + scoreTwo + scoreThree) / 3.0f);
return average;
}

public int getHighScore(){
//Determine and return the highest score
int highScore;
highScore = scoreOne;
if (scoreTwo > highScore) highScore = scoreTwo;
if (scoreThree > highScore) highScore = scoreThree;
return highScore;
}

private Student(){
scoreOne=86;
scoreTwo=90;
scoreThree=73;
}
public String toString(){
//Construct and return a string representation of the student
String str;
str = "Name: " + name + "\n" +
"Test 1: " + scoreOne + "\n" +
"Test 2: " + scoreTwo + "\n" +
"Test 3: " + scoreThree + "\n" +
"Average: " + getAverage();
return str;
}
}

Hi ComputerGirl and welcome to DaniWeb :)

You have two problems that I can see. The main problem is in your main method (excuse the pun).

public static void main(String[] args) {
  Student demo = new Student();
  demo.setSize(250, 400); // ---!!! demo is of type Student which does not have a setSize method
  // that call should be in your createGUI() method and called by window
  demo.setTitle("Test Scores"); // ---!!! same here
  demo.createGUI();
  demo.setVisible(true); // ---!!! same here
}

Also, you have two constructor methods for Student, one public which sets all member variables to default settings even though parameters were supplied, and one private (not sure why you need this one?)

And post your code in code tags in the future. You might want to look at the forum rules/stickies.

@darkagn
Student does have all those methods because it extends JFrame.
@ComputerGirl
Are you sure you ran it? When I run it I see the window but other than that it doesn't do anything yet.

@darkagn
Student does have all those methods because it extends JFrame.

Ah, I missed that, jasimp is correct on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.