Can someone help me with my code.
Code :
import java.util.ArrayList;
import javax.swing.*;
import java.text.*;
import java.awt.*;
public class Student
{
private String id = new String ();
private String name = new String ();
private double gpa;
private Student [] person;
private Student [] answers;
private Student [] questions;
Student() //Constructor
{
id = "Unknown ID";
name = "Unknown Name";
gpa = 0;
}
Student (String idnum, String n, double g)
{
id = idnum;
name = n;
gpa = g;
}
Student (int howManyKids, int howManyAnswers, String idnum, String n, double g)
{
id = new String(idnum);
name = new String(n);
double gpa = g;
int i = 0;
int a =0;
person = new Student[howManyKids];
for (i=0; i < howManyKids; ++i)
person [i] = new Student();
answers = new Student[howManyAnswers];
for (a=0; a < howManyAnswers; ++a)
answers [a] = new Student();
//populate (howManyKids);
//populate (howManyAnswers);
}
public void setAllStudentFields (int howManyKids, int howManyAnswers, String idnum, String n, double g)
{
id = new String(idnum);
name = new String(n);
double gpa = g;
int i = 0;
int a =0;
person = new Student[howManyKids];
for (i=0; i < howManyKids; ++i)
person [i] = new Student();
answers = new Student[howManyAnswers];
for (a=0; a < howManyAnswers; ++a)
answers [a] = new Student();
//populate (howManyKids);
//populate (howManyAnswers);
}// end of setter to setAllStudentFields
public static void teacherTest(ArrayList questions)
{
boolean valid = false;
ArrayList<Character> key = new ArrayList<Character>();
if ((one != 'T') || (one != 'F'))
questions.add(new Character(one));
else
{
System.out.println("Not an accpetable answer! Only enter a T or F.");
valid = false;
while (valid == false)
{
System.out.println("(Only enter a T or F)");
System.out.println("Question number " + i + " -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
{
questions.add(new Character(one));
valid = true;
}//end of if statement
}
}
while (one != 'X')
{
key.add(new Character(one));
System.out.println("Enter the answer to question -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
}
}
public static void testForOneStudent(ArrayList answers)
{
char one = ' ';
Character torf;
int i = 0;
int correct = 0;
boolean valid = false;
for (i = 1; i <= 5; ++i)
{
System.out.println("(Only enter a T or F)");
System.out.println("Question number " + i + " -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
answers.add(new Character(one));
else
{
System.out.println("Not an accpetable answer! Only enter a T or F.");
valid = false;
while (valid == false)
{
System.out.println("(Only enter a T or F)");
System.out.println("Question number " + i + " -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
{
answers.add(new Character(one));
valid = true;
}//end of if statement
}//end of while loop
}//end of else path
}// end of for loop
}//end of public
public String getName()
{
return name;
}
public String getID()
{
return id;
}
public double getGPA()
{
return gpa;
}
public void setName(String n)
{
name = n;
}
public void setID(String idnum)
{
id = idnum;
}
public void setGPA(double g)
{
gpa = g;
}
public Student[] getPersons()
{
return person;
}
public Student[] getAnswers()
{
return answers;
}
public String toPrintPersonArray()
{
String pOutput = new String();
for (int i = 0; i < person.length; i++)
{
pOutput += "The number of kids are " + (i + 1) + "\nand they are " + person[i].toString() + "\n";
}
return pOutput;
}
public String toPrintAnswerArray()
{
String aOutput = new String();
for (int i = 0; i < answers.length; i++)
{
aOutput += "The number of answers received are " + (i + 1) + "\nand they are " + answers[i].toString() + "\n";
}
return aOutput;
}
public String toString()
{
return ("\nName --> " + getName() + " \t Id Number --> " + getID() +
" \t GPA --> " + getGPA() + "\n" + toPrintPersonArray() +
toPrintAnswerArray());
}
}
I'm creating a program that has a user input a teacher key and the student's answer to output both the key, the students answer, student name, id, and gpa. I'm in the process of writing a test class but need to figure out the errors to this one first. This needs to be array based as well.
Re: Can someone help me with my code.
We can't really help you with the errors until you tell us what those errors are. I suggest you throw together an SSCCE that demonstrates one problem at a time without any extra code, and we'll go from there.
Re: Can someone help me with my code.
Quote:
Originally Posted by
KevinWorkman
We can't really help you with the errors until you tell us what those errors are. I suggest you throw together an
SSCCE that demonstrates one problem at a time without any extra code, and we'll go from there.
I changed my code. I restarted because I couldn't figure out what the errors were.
here is my main program so far.
Code :
import java.util.ArrayList;
import javax.swing.*;
import java.text.*;
import java.awt.*;
public class NewStudent
{
private String id = new String ();
private String name = new String ();
private double gpa;
private NewStudent [] person;
private NewStudent [] answers;
private NewStudent [] questions;
NewStudent() //Constructor
{
id = "Unknown ID";
name = "Unknown Name";
gpa = 0;
}
NewStudent (int howManyKids, int howManyAnswers, String idnum, String n, double g)
{
id = new String(idnum);
name = new String(n);
double gpa = g;
int i = 0;
int a =0;
person = new NewStudent[howManyKids];
for (i=0; i < howManyKids; ++i)
person [i] = new NewStudent();
answers = new NewStudent[howManyAnswers];
for (a=0; a < howManyAnswers; ++a)
answers [a] = new NewStudent();
//populate (howManyKids);
//populate (howManyAnswers);
}
public void setAllStudentFields (int howManyKids, int howManyAnswers, String idnum, String n, double g)
{
id = new String(idnum);
name = new String(n);
double gpa = g;
int i = 0;
int a =0;
person = new NewStudent[howManyKids];
for (i=0; i < howManyKids; ++i)
person [i] = new NewStudent();
answers = new NewStudent[howManyAnswers];
for (a=0; a < howManyAnswers; ++a)
answers [a] = new NewStudent();
//populate (howManyKids);
//populate (howManyAnswers);
}// end of setter to setAllStudentFields
public static void teacherTest(ArrayList questions)
{
boolean valid = false;
ArrayList<Character> key = new ArrayList<Character>();
System.out.println("(Only enter a T or F for the Teacher's Key)");
System.out.println("Enter the answer to question -->");
char one = ' ';
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
questions.add(new Character(one));
else
{
System.out.println("Not an accpetable answer! Only enter a T or F.");
valid = false;
while (valid == false)
{
System.out.println("(Only enter a T or F)");
System.out.println("Enter the answer to question -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
{
questions.add(new Character(one));
valid = true;
}//end of if statement
}
}
while (one != 'X')
{
key.add(new Character(one));
System.out.println("Enter the answer to question -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
}
}
public static void testForOneStudent(ArrayList answers)
{
char one = ' ';
Character torf;
int i = 0;
int correct = 0;
boolean valid = false;
for (i = 1; i <= 5; ++i)
{
System.out.println("(Only enter a T or F)");
System.out.println("Question number " + i + " -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
answers.add(new Character(one));
else
{
System.out.println("Not an accpetable answer! Only enter a T or F.");
valid = false;
while (valid == false)
{
System.out.println("(Only enter a T or F)");
System.out.println("Question number " + i + " -->");
one = Character.toUpperCase(GetInput.readLineNonwhiteChar());
if ((one != 'T') || (one != 'F'))
{
answers.add(new Character(one));
valid = true;
}//end of if statement
}//end of while loop
}//end of else path
}// end of for loop
}//end of public
public String getName()
{
return name;
}
public String getID()
{
return id;
}
public double getGPA()
{
return gpa;
}
public void setName(String n)
{
name = n;
}
public void setID(String idnum)
{
id = idnum;
}
public void setGPA(double g)
{
gpa = g;
}
public String toPrintPersonArray()
{
String pOutput = new String();
for (int i = 0; i < person.length; i++)
{
pOutput += "The number of kids are " + (i + 1) + "\nand they are " + person[i].toString() + "\n";
}
return pOutput;
}
public String toPrintAnswerArray()
{
String aOutput = new String();
for (int i = 0; i < answers.length; i++)
{
aOutput += "The number of answers received are " + (i + 1) + "\nand they are " + answers[i].toString() + "\n";
}
return aOutput;
}
public String toString()
{
return ("\nName --> " + getName() + " \t Id Number --> " + getID() +
" \t GPA --> " + getGPA() + "\n" + toPrintPersonArray() +
toPrintAnswerArray());
}
}
That compiles ok but my test class can't run it.
Code :
import java.util.ArrayList;
import javax.swing.*;
import java.text.*;
import java.awt.*;
public class TestStudent
{
public static void main(String args[])
{
String name;
String id;
double gpa;
NewStudent john = new NewStudent();
john.teacherTest();
}
}
Can anyone help me get this running?
Re: Can someone help me with my code.
When you say that your test class can't run it, what exactly do you mean? Do you have compiler errors? Something else?
Re: Can someone help me with my code.
It says there is a problem with this line of code. john.teacherTest();
Re: Can someone help me with my code.
Left click the black screen that displays the error message.
Click on the select all option.
On your keyboard hit Ctrl + c.
Come back here and open a new reply and hit Ctrl + v.
Re: Can someone help me with my code.
You are trying to use a non-static method called teacherTest that does not take any parameters. Where do you have that method defined? Hint: you don't.