Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-18-2008, 03:47 PM
Member
 
Join Date: Jun 2008
Posts: 11
buzzdsm is on a distinguished road
Array help please
I have an assignment that I'm stumped on. I created a class and got it to work for one student but I can't figure it out with multiple students. I've included what I currently have. Here is what they are asking for.


Write a class that uses a string array to hold the 5 students names, an array of 5 characters to hold the 5 students letter grades, and 5 arrays of four doubles each to hold each students test scores.

Code:
import java.util.Scanner; public class GradeBook { //declare variables private String names; private int scores1; private int scores2; private int scores3; private int scores4; private double average; private char grades; Scanner keyboard = new Scanner(System.in); //default constructor public GradeBook() { //initialize variables names = ""; scores1 = 0; // Test Score 1 scores2 = 0; // Test Score 2 scores3 = 0; // Test Score 3 scores4 = 0; // Test Score 4 average = 0; // total of 4 test scores grades = 'A'; } //overloaded constructor public GradeBook (String inName, int scr1, int scr2, int scr3, int scr4, int scr5, int sAverage, char sLetterGrade) { names = inName; scores1 = scr1; scores2 = scr2; scores3 = scr3; scores4 = scr4; average = sAverage; grades = sLetterGrade; } public String toString() { return "Name: " +names+ "\t" + "Average score: " +average+ "\t" + "Grade: " + grades; } public void display() { Scanner keyboard = new Scanner(System.in); System.out.println(toString()); } //names setter public void setnames(String student) { names = student; } // scores1 setter public void setscores1(int S1) { scores1 = S1; } //scores2 setter public void setscores2(int S2) { scores2 = S2; } //scores3 setter public void setscores3(int S3) { scores3 = S3; } //scores4 setter public void setscores4(int S4) { scores4 = S4; } //average setter public void setaverage(int scores1, int scores2, int scores3, int scores4) { average = scores1 + scores2 + scores3 + scores4; } //grades setter public void setgrades(char gradesScore) { grades = gradesScore; } //names getter public String getnames() { return names; } // scores1 getter public int getscores1() { return scores1; } //scores2 getter public int getscores2() { return scores2; } //scores3 getter public int getscores3() { return scores3; } //scores4 getter public int getscores4() { return scores4; } //average getter public double getaverage() { return average; } //grades getter public char getgrades() { return grades; } public void startProcessing() { String student = ""; System.out.println("Enter student 1's name:"); student = keyboard.next(); setnames(student); System.out.println("Now enter student 1's four test scores"); System.out.println(""); System.out.println(""); System.out.println(""); scores1=retrieveScore("Test Score #1", 100); scores2=retrieveScore("Test Score #2", 100); scores3=retrieveScore("Test Score #3", 100); scores4=retrieveScore("Test Score #4", 100); average=grades(); System.out.println(""); } public int retrieveScore(String sVal, int maxVal) { int x; System.out.println(sVal + ":"); x = keyboard.nextInt(); if ((x>=0)&&(x<=maxVal)) { System.out.println(" "); } else {//If not valid test score System.out.println("Invalid test score"); System.exit(0); } return x; } public double grades() { int sumTest; double percentTest; sumTest = scores1+ scores2+ scores3+ scores4; percentTest = ((sumTest)/4.0); average = percentTest; if(average>=90) grades='A'; else if(average >=80) grades='B'; else if(average>=70) grades='C'; else if(average>=60) grades='D'; else if(average<60) grades='F'; return average; }

Code:
public class gradeDemo { public static void main(String[] arg) { // GradeBook gradingTest = new GradeBook(); // gradingTest.display(); GradeBook gradingTest = new GradeBook(); gradingTest.startProcessing(); System.out.println(gradingTest.toString()); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-18-2008, 08:22 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
public class GradeBook { //declare variables // a string array to hold the 5 students names private String[] names = new String[5]; // an array of 5 characters to hold the 5 students letter grades private char[] grades = new char[5]; // 5 arrays of four doubles each to hold each students test scores private double[] scores1 = new double[4]; private double[] scores2 = new double[4]; private double[] scores3 = new double[4]; private double[] scores4 = new double[4]; private double[] scores5 = new double[4];
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-18-2008, 08:43 PM
Member
 
Join Date: Jun 2008
Posts: 11
buzzdsm is on a distinguished road
Thanks a ton hardwired.

Is there something I need to take out of my class for this to work?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-19-2008, 01:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
I would start over and build your class so that it does what you want it to do (according to your assignment).
For example:
Code:
import java.util.Scanner; public class GradeBookRx { //declare variables // a string array to hold the 5 students names private String[] names = new String[5]; // an array of 5 characters to hold the 5 students letter grades private char[] grades = new char[5]; // 5 arrays of four doubles each to hold each students test scores private double[] scores1 = new double[4]; private double[] scores2 = new double[4]; private double[] scores3 = new double[4]; private double[] scores4 = new double[4]; private double[] scores5 = new double[4]; int count = 0; Scanner keyboard = new Scanner(System.in); public void enterData() { System.out.println("Enter student's name:"); String student = keyboard.nextLine(); System.out.println("Now enter student's first test score"); double score1 = Double.parseDouble(keyboard.nextLine()); System.out.println("Now enter student's second test score"); double score2 = Double.parseDouble(keyboard.nextLine()); System.out.println("Now enter student's third test score"); double score3 = Double.parseDouble(keyboard.nextLine()); System.out.println("Now enter student's fourth test score"); double score4 = Double.parseDouble(keyboard.nextLine()); addStudent(student, score1, score2, score3, score4); } public String toString() { StringBuilder sb = new StringBuilder(); for(int i = 0; i < names.length; i++) { if(names[i] == null) continue; sb.append("Name: " + names[i] + "\t" + "Average score: " + getAverage(i) + "\t" + "Grade: " + grades[i] + "\n"); } return sb.toString(); } private void addStudent(String inName, double scr1, double scr2, double scr3, double scr4) { // If arrays are full, start over at beginning. if(count == names.length-1) count = 0; names[count] = inName; scores1[count] = scr1; scores2[count] = scr2; scores3[count] = scr3; scores4[count] = scr4; grades[count] = getGrade(count); count++; } private double getAverage(int index) { double score1 = scores1[index]; double score2 = scores2[index]; double score3 = scores3[index]; double score4 = scores4[index]; double average = (score1 + score2 + score3 + score4)/4.0; return average; } private char getGrade(int index) { double average = getAverage(index); char grade; if(average>=90) grade='A'; else if(average >=80) grade='B'; else if(average>=70) grade='C'; else if(average>=60) grade='D'; else // average<60 grade='F'; return grade; } public static void main(String[] argsd) { GradeBookRx test = new GradeBookRx(); test.enterData(); System.out.println("gradebook = " + test); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with an array. f_the_cook New To Java 7 06-03-2008 06:05 AM
Array Reflection: Multi Array Reflection Java Tip java.lang 0 04-23-2008 10:08 PM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 10:59 PM
2D array bluekswing New To Java 2 01-15-2008 07:57 PM
Help with Array susan New To Java 1 08-07-2007 06:32 AM


All times are GMT +3. The time now is 12:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org