1

Basically what I am overall trying to do is:

One of your professors hears of your emerging programming expertise and asks you to write a SINGLE program that can be used to help them with their grading. The professor gives three 50-point exams and a single 100-point final exam. Your program will prompt the user for the student’s name, entered as Firstname Lastname (i.e. Bob Smith), the student’s 3-exam scores and 1-final exam score (all whole numbers). Class size varies from semester to semester, but 100 is the limit (declare as a constant).

Read in information for ALL students before doing any calculations or displaying any output. Verify that the 3 exam scores are between 0-50 points and that the final is between 0-100 as they are entered. Declared minimums and maximums as constant so that they can easily be updated, as needed. If invalid, display an error message and allow the user to re-enter that invalid score. Once all student info is read in, display each student’s name in the format LASTNAME, FIRSTNAME (all uppercase), the student’s exam percentage (total of all exams plus final / total possible) to 1 decimal and the student’s final grade.

But i am having trouble figuring out how to assign the user inputs into an array ( or 2 arrays maybe because of first and last name?) but I'm pretty lost on what to do, This is what i have now:

import java.util.*;
import java.text.*;


public class Proj4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
String again = "y";

int [] exams = new int[4];
int student = 1;

do
{

    String [] names = new String[student];
        System.out.print("PLease enter the name of student " + student + ": " );
        names[student-1] = s.nextLine();
        for ( int i = 0; i < exams.length; i++){
            if(i==3){
                System.out.print("Please enter score for Final Exam: ");
                exams[i] = s.nextInt();
            }
            else{
            System.out.print("Please enter score for Exam " + (i+1) + ": ");
            exams[i] = s.nextInt(); 

                if((exams[0]<0||exams[0]>50)||(exams[1]<0||exams[1]>50)||(exams[2]<0||exams[2]>50)){
                    System.out.println("Invalid enter 0-50 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
                else if(exams[3]<0||exams[3]>100){
                    System.out.println("Invalid enter 0-100 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
            }
        }
        System.out.print("do you wish to enter another? (y or n) ");
        again = s.nextLine();
        if(again!="y")
            student++;
}while (again.equalsIgnoreCase ("y"));
}
}

If there's anything else wrong with my code as well, help with that would be awesome too.

2
  • Are you allowed to use lists instead of arrays? Commented Mar 1, 2013 at 4:56
  • I don't think so, we haven't learned that in my class yet i don't think, but if its not possible with out doing that then maybe.. Commented Mar 1, 2013 at 4:59

1 Answer 1

2

First, declare the constants that are specified by the requirements:

final int MAX_STUDENTS = 100; 
final int MIN_EXAM = 0; 
final int MAX_EXAM = 50; 
final int MIN_FINAL = 0; 
final int MAX_FINAL = 100.  

Then, since you are only allowed to use arrays, declare two arrays. One will hold the student names and the other will hold the test scores. For this reason, they will be of different types. Initialize the String array to be the maximum number of students. Initialize test scores array to be 4 * MAX_STUDENTS, since each student will have 4 exam scores:

String[] student_names = new String[MAX_STUDENTS];
int[] exam_scores = new int[MAX_STUDENTS * 4];

When you read in a new student, put his/her name in a new index of the student_names array. Then, as you read in each subsequent test score (3 exams, 1 final), incrementally fill the exam_scores array.

When you go to print out the scores, keep a variable that keeps track of the last index of the the exam_scores array that was printed. That way, when you move to another student (as you are iterating through the array), you know where you left off in the exam_scores array.

There are other (better) ways of doing this (e.g. using lists is best, but even with arrays you could get fancier than this). I wasn't sure what you had learned so far so chose the most basic way to implement the program.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this actually does look familiar, I had forgotten about the max min constant requirements but looking at what you have put it makes sense now, and i think i can probably figure it out from here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.