samedi 28 février 2015

How to search for an element in an array? and How to add variables with declared methods into an array list?



I have 2 major troubles (that I'm aware of) with this program. Firstly, I don't know how to get FinalGrade and LetterGrade into the array. Secondly, I don't know how to use last name and first name to search for a student in the array. Let me know if you find other problems with my program. Thanks


This is the 1st class



package student;

public class Person {

protected String FirstName, LastName;

//Constructor
public Person(String FirstName, String LastName) {
this.FirstName = FirstName;
this.LastName = LastName;
}

//Getters
public String getFirstName() {
return FirstName;
}

public String getLastName() {
return LastName;
}
//Setters
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}

public void setLastName(String LastName) {
this.LastName = LastName;
}


}


This is the 2nd class:



package student;

import java.util.ArrayList;
import java.util.Scanner;



public class Student extends Person{
private int HomeworkAve, QuizAve, ProjectAve, TestAve;
private double FinalGrade;
private String LetterGrade;

//Constructor for the averages
public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, String FirstName, String LastName)
{
super(FirstName, LastName);
this.HomeworkAve = HomeworkAve;
this.QuizAve = QuizAve;
this.ProjectAve = ProjectAve;
this.TestAve = TestAve;

}

//Method to calculate final grade and letter grade
//Final grade calculation
public double CalcGrade (int HomeworkAve, int QuizAve, int ProjectAve, int TestAve)
{
FinalGrade = (double)(0.15*HomeworkAve + 0.05*QuizAve + 0.4 * ProjectAve + 0.4*TestAve);
return FinalGrade;
}

//Letter grade calculation
public String CalcGrade ( double FinalGrade)
{
if ( FinalGrade >= 90.00)
LetterGrade="A";
else if(FinalGrade >= 80.00)
LetterGrade="B";
else if(FinalGrade>=70.00)
LetterGrade="C";
else if(FinalGrade>=60.00)
LetterGrade="D";
else LetterGrade="F";

return LetterGrade;
}

public String getFullName (String FirstName,String LastName)
{
String str1 = FirstName;
String str2 = LastName;
String FullName = str1+","+str2;
return FullName;
}

public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, double FinalGrade, String LetterGrade, String FirstName, String LastName) {
super(FirstName, LastName);
this.HomeworkAve = HomeworkAve;
this.QuizAve = QuizAve;
this.ProjectAve = ProjectAve;
this.TestAve = TestAve;
this.FinalGrade = FinalGrade;
this.LetterGrade = LetterGrade;

}


//Setters for this student class
public void setHomeworkAve(int HomeworkAve) {
this.HomeworkAve = HomeworkAve;
}

public void setQuizAve(int QuizAve) {
this.QuizAve = QuizAve;
}

public void setProjectAve(int ProjectAve) {
this.ProjectAve = ProjectAve;
}

public void setTestAve(int TestAve) {
this.TestAve = TestAve;
}

public void setFinalGrade(int FinalGrade) {
this.FinalGrade = FinalGrade;
}

public void setLetterGrade(String LetterGrade) {
this.LetterGrade = LetterGrade;
}


//Getters for this student class
public int getHomeworkAve() {
return HomeworkAve;
}

public int getQuizAve() {
return QuizAve;
}

public int getProjectAve() {
return ProjectAve;
}

public int getTestAve() {
return TestAve;
}

public double getFinalGrade() {
return FinalGrade;
}

public String getLetterGrade() {
return LetterGrade;
}

public void DisplayGrade (){
System.out.println(FirstName+" "+LastName+"/nFinal Grade: "+FinalGrade+"/nLetter Grade: "+ LetterGrade);
}




public static void main(String[] args){
Scanner oScan = new Scanner(System.in);
Scanner iScan = new Scanner(System.in);

boolean bContinue = true;
int iChoice;

ArrayList<Student> students = new ArrayList<>();


while (bContinue == true)
{
//The menu
System.out.println("1. New Class List");
System.out.println("2. Search for a Student");
System.out.println("3. Exit");
System.out.println("Choose an item");

iChoice = iScan.nextInt();


//The 1st case: when the user wants to enter the new list
if (iChoice == 1){

System.out.println("Enter the number of students");

int numberOfStudents = iScan.nextInt();

for(int iCount = 0;iCount < numberOfStudents;){

System.out.println("Enter the name for Student " + ++iCount);
System.out.println("Enter First Name");
String FirstName = oScan.nextLine();
System.out.println();

System.out.println("Enter Last Name");
String LastName = oScan.nextLine();
System.out.println();

System.out.println("Enter Homework Average");
int HomeworkAve = iScan.nextInt();
System.out.println();

System.out.println("Enter Quiz Average");
int QuizAve = iScan.nextInt();
System.out.println();

System.out.println("Enter Project Average");
int ProjectAve = iScan.nextInt();
System.out.println();

System.out.println("Enter Test Average");
int TestAve = iScan.nextInt();
System.out.println();


How to get FinalGrade and LetterGrade??



Student hobbit = new Student(HomeworkAve,QuizAve, ProjectAve,TestAve,FirstName, LastName);
students.add(hobbit);
}
}


//The 2nd case: when the user wants to search for a student
else if (iChoice == 2)
{
System.out.println("Enter First Name");
String FirstName = oScan.nextLine();
System.out.println();

System.out.println("Enter Last Name");
String LastName = oScan.nextLine();
System.out.println();


below is my way to find the student in the array but it doesn't work, I type exactly the first name and last name but it said "Not found"



int iFound1, iFound;
iFound1 = students.indexOf(FirstName);
iFound2 = students.indexOf(LastName);
if (iFound1 >= 0 & iFound2 >= 0)
System.out.println("The student is " + students.get(iFound1)+ students.get(iFound2) );
else
System.out.println("\n\nNot found!");

}


//The 3r case: when the user wants to exit
else if (iChoice == 3)
{
bContinue = false;
}
}
}

}



Aucun commentaire:

Enregistrer un commentaire