6 Replies - 1271 Views - Last Post: 02 November 2009 - 06:00 PM Rate Topic: -----

#1 yolifeisGOOD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Student grade convertor

Post icon  Posted 02 November 2009 - 03:45 PM

this is what im supposed to do....

1. Write a class called Student that has the following instance variables: id, name, address, examScore. Create appropriate constructors. The class also includes the following methods:

a. setExamScore(double score)  sets the examScore variable of the student object to the score that is passed as a parameter
b. computeGrade  uses the student’s examScore to determine a letter grade for the student. You may use standard grading procedures (>= 90 is an A, >= 80 and <90 is a B, and so on….)
c. toString()  allows one to display the student object as a string. This should return the id, name, examScore, and letter grade of the student as a string.

class Student {
	public String name;
	public String address;
	public int id;
	public double examScore;


	public Student(){ //default constructor
	} 
		
	public Student(int stId, String stName, String stAddress, char stGrade){
		id = stId;
		name = stName;
		address = stAddress;
		examScore = stGrade;
	} 
		
	public int getId(){
		return id;
	}
	
	public void setId(int stId){
		if(stId>0)
			id = stId; //if the student id is greater than 0 pass it to stID and set it to id "instance variable from main"
			
	}
	
	public String getName(){
		return name;
	}
	public void setName(String stName){
		name = stName;
	}
	
	public String getAddress(){
		return address;
	}
	public void setAddress(String stAddress){
		address = stAddress;
	}
	
	public double getexamScore(){
		return examScore;
	}
	
	public void setexamScore(double stGrade){
	 	if(examScore>=90)
	 		stGrade='A';
	 	else if(examScore>=80 && examScore <90)
	 		stGrade='B';
	 	else if(examScore>=70 && examScore <80)
	 		stGrade='C';
	 	else if(examScore>=60 && examScore <70)
	 		stGrade='D';
	 	else stGrade ='F';
	 
	 	examScore = stGrade;
	}
	
	public char computeexamScore(char stGrade){

	 	if(examScore>=90)
	 		stGrade='A';
	 	else if(examScore>=80 && examScore <90)
	 		stGrade='B';
	 	else if(examScore>=70 && examScore <80)
	 		stGrade='C';
	 	else if(examScore>=60 && examScore <70)
	 		stGrade='D';
	 	else stGrade ='F';
	 
	 	return stGrade;
	}
		
	public String toString(){
		String st;
		//integer type use %d, string is %s
		st=String.format("Name: %s\nID: %d\nAddress: %s\nExam-Score: %s\n",
						name,id,address,examScore);
		return st;
	}
}


and to test
public class TestStudent {
	public static void main(String[] args) {
		
		Student Ask = new Student();
		Ask.name = "yes sir";
		Ask.setId(100033333);
		Ask.address = "2010 Graduation Street, somwhere, Texas 66666";
		Ask.setexamScore(100);
			
		System.out.println(Ask);
		
		Student ask2 = new Student();
		ask2.name = "Jane Winterspoon";
		ask2.setId(1000635111);
		ask2.address = "2000 Vacation blvd, Somewhere, Texas 99999";
		ask2.setexamScore(87);
		
		System.out.println(ask2);

	}
}




i just can't seem to get the grade to convert into a letter like for the first one it would display "A"

Is This A Good Question/Topic? 0
  • +

Replies To: Student grade convertor

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Student grade convertor

Posted 02 November 2009 - 04:01 PM

hi
i am confuse by the two methods:
setexamScore(double stGrade) and computeexamScore(char stGrade)
should they work together? what is the functionality of each of them?
anyways, i think the problem is that your computeexamScore methods expects a char variable.
i dont think it should get any variable, since the char for the grade is calculated within the method.
Was This Post Helpful? 0
  • +
  • -

#3 yolifeisGOOD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: Student grade convertor

Posted 02 November 2009 - 04:45 PM

basically what im asking is how do i pass an integer grade to the method and have it return a letter grade,

the computeexamScore() is suppose to compute the grade and return the letter grade but i think im not coding it correctly
Was This Post Helpful? 0
  • +
  • -

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Student grade convertor

Posted 02 November 2009 - 04:59 PM

your method is ok, just it should get an nt value instead a char.
here is a sample program using your method with that little change:
notice the method is declared static, it is just so i could call it inside the main method.
public class Class1 {
public static void main(String[] args){
int[] exams = {100,20,90,89,45,68,76,45,34};
for(int i=0;i<exams.length;i++){
	System.out.println(exams[i] +"\t"+computeexamScore(exams[i]));
}
}
public static char computeexamScore(int examScore){//it gets an int value since examScore is an int (0-100)
//you could also use double..
	char stGrade;//the char variable to hold the appropriate letter
		 if(examScore>=90)
			 stGrade='A';
		 else if(examScore>=80 && examScore <90)
			 stGrade='B';
		 else if(examScore>=70 && examScore <80)
			 stGrade='C';
		 else if(examScore>=60 && examScore <70)
			 stGrade='D';
		 else stGrade ='F';
	 
		 return stGrade;//return the char variable
	}

}



check my remarks where i made the changes.
run the program, it is very simple.
understand how it works and use it within your code.
Was This Post Helpful? 0
  • +
  • -

#5 yolifeisGOOD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: Student grade convertor

Posted 02 November 2009 - 05:24 PM

i still can't figure out why my grade won't convert to a letter grade, i guess what im trying to do is

getexamScore(); //to return the exam score
setexamScore(); // to set the exam score
computeexamScore(); //to calculate and return a lettergrade

i just can't seem to get it to pass into those method
Was This Post Helpful? 0
  • +
  • -

#6 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Student grade convertor

Posted 02 November 2009 - 05:41 PM

hi

setExamScore should only do:
public void setExamScore(int score){
this.examScore = score;
}


then, computeexamScore will use the examScore variable already declare in Stident class.

here is an updated Student class.
just copy and run it.
see my remarks.
class Student {
	public String name;
	public String address;
	public int id;
	public int examScore;//for the examScore


	public Student(){ //default constructor
	} 

	public Student(int stId, String stName, String stAddress, int stExamScore){
		this.id = stId;
		this.name = stName;
		this.address = stAddress;
		this.examScore = stExamScore;//get the examScore in the Constructor
	} 
		
	public int getId(){
		return id;
	}
	
	public void setId(int stId){
		if(stId>0)
			id = stId; //if the student id is greater than 0 pass it to stID and set it to id "instance variable from main"
			
	}
	
	public String getName(){
		return name;
	}
	public void setName(String stName){
		name = stName;
	}
	
	public String getAddress(){
		return address;
	}
	public void setAddress(String stAddress){
		address = stAddress;
	}
	
	public double getexamScore(){
		return examScore;
	}
	
	public void setExamScore(int score){
this.examScore = score;//for updating examScore.
}
	
  public char computeexamScore(){
//the examScore value is th value you get ffrom the parameter passed to your constructor.
	char stGrade;//the char variable to hold the appropriate letter
		 if(examScore>=90)
			 stGrade='A';
		 else if(examScore>=80 && examScore <90)
			 stGrade='B';
		 else if(examScore>=70 && examScore <80)
			 stGrade='C';
		 else if(examScore>=60 && examScore <70)
			 stGrade='D';
		 else stGrade ='F';
	 
		 return stGrade;
	}
		
	public String toString(){
		String st;
		//integer type use %d, string is %s
		st=String.format("Name: %s\nID: %d\nAddress: %s\nExam-Score: %s\n",
						name,id,address,computeexamScore());
		return st;
	}
public static void main(String args[]){
Student st= new Student(998877, "st","nowhere", 100);
System.out.println(st);
}
}


hope it helps :)
Was This Post Helpful? 0
  • +
  • -

#7 yolifeisGOOD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 09

Re: Student grade convertor

Posted 02 November 2009 - 06:00 PM

oh my..thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1