Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Returning Boolean Values

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 5 Times in 1 Post

    Default Returning Boolean Values

    Hi,
    I'm fairly new to java, this code is for an assignment in an intro to CompSci class. I've had help (as can be seen in some comments) for some of this code but I'm having trouble with the last bit. The last of the assignment spec's are here


    Print the results in exactly the following format:

    3.[Last Name], [First Name]
    Student ID: [Student ID]
    Homework: [Homework Score]
    Quizzes: [Quizzes Score]
    Midterm: [Midterm Score]
    Final: [Final Score] AND, on another line, Grade: [Final Grade]

    4.Prompt the user to either enter another student or to quit. Continue the process until the user chooses to quit.

    the code so far is here

    import java.util.*;
     
    public class GradeCount{
     
    	public static void main(String[] args)
    	{
    		boolean quit = false;
     
    		/**
    		 * Initialize student data variables
    		 */
    		int studentId;
    		String firstName;
    		String lastName;
    		double assignments,
    				quizzes,
    				midterm,
    				finalExam;
    		int totalScore;
    		String letterGrade;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		/**
    		 * Prompt user to enter another student or to quit
    		 * continue process until the user chooses to quit
    		 */
    		while(quit == false)
    		{
    			// Get student information
    			studentId = getStudentId(keyboard);
    			firstName = getFirstName(keyboard);
    			lastName = getLastName(keyboard);
    			assignments = getAssignments(keyboard);
    			quizzes = getQuizzes(keyboard);
    			midterm = getMidterm(keyboard);
    			finalExam = getFinalExam(keyboard);
     
    			// Calculate student grade
    			totalScore = getTotalScore(assignments, quizzes, midterm, finalExam);
    			letterGrade = getLetterGrade(totalScore);
     
    			// Display results to the user
    			displayresults(studentId, firstName, lastName, assignments, quizzes, midterm, finalExam, totalScore, letterGrade);
     
    			quit = promptContinue();
    		}
    	}
     
    	public static int getStudentId(Scanner keyboard)
    	{
    		System.out.println("Please enter the student id");
    		System.out.print("> ");
    		int studentId = keyboard.nextInt();
     
    		// Skip the newline
     
    	keyboard.nextLine();
     
    		return studentId;
    	}
     
    	public static String getFirstName(Scanner keyboard)
    	{
    		System.out.println("Please enter the student first name");
    		System.out.print("> ");
    		return keyboard.nextLine();
    	}
     
    	public static String getLastName(Scanner keyboard)
    	{
    		System.out.println("Please enter the student last name");
    		System.out.print("> ");
    		return keyboard.nextLine();
    	}
     
     
    	public static int getAssignments(Scanner keyboard)
    	{
    		System.out.println("Please enter the student assignments");
    		System.out.print("> ");
    		return keyboard.nextInt();
    	}
     
     
    	public static int getQuizzes(Scanner keyboard)
    	{
    		System.out.println("Please enter the student quiz score");
    		System.out.print("> ");
    		return keyboard.nextInt();
     
    	}
     
    	public static int getMidterm(Scanner keyboard)
    	{
    		System.out.println("Please enter the student midterm score");
    		System.out.print("> ");
    		return keyboard.nextInt();
    	}
     
    	public static int getFinalExam(Scanner keyboard)
    	{
    		System.out.println("Please enter the student final exam score");
    		System.out.print("> ");
    		return keyboard.nextInt();
    	}
     
     
    	public static int getTotalScore(double assignments, double quizzes, double midterm, double finalExam)
    	{
    		// Use a double to get full accuracy
    		double totalScore = (assignments + quizzes + midterm + finalExam) / 4;
     
    		// Cast it back to an int which makes more sense with our logic
    		return (int) totalScore;
    	}
     
    	/**
    	 * Calculate a total score for the student & assign a letter grade
    	 * 90 - 100 = A
    	 * 80 - 89 = B
    	 * 70 - 79 = C
    	 * 60 - 69 = D
    	 * 59 and below = E
    	 */
    	public static String getLetterGrade(int totalScore)
    	{
    		if(totalScore >= 90 && totalScore <= 100)
    		{
    			return "A";
    		}
    		else if(totalScore >= 80 && totalScore <= 89)
    		{
    			return "B";
    		}
    		else if(totalScore >= 70 && totalScore <= 79)
    		{
    			return "C";
    		}
    		else if(totalScore >= 60 && totalScore <= 69)
    		{
     
    	return "D";
    		}
    		else if(totalScore <= 59)
    		{
    			return "E";
    		}
     
    		// This should never happen
    		return "N/A";
    	}
     
    	/**
    	 * Print the results in the following format
    	 * [Last Name], [First Name]
    	 * Student ID: [Student ID]
    	 * Homework: [Homework Score]
    	 * Quizzes: [Quizzes Score]
    	 * Midterm: [Midterm Score]
    	 * Final: [Final Score]
    	 * Grade: [Final Grade]
    	 */
     
    	public static void displayResults()
    	{
    		// Use print and / or println to display the final results for the students
    		System.out.print([lastName], [firstName]);
    		System.out.println(studentId);
    		System.out.println(assignments);
    		System.out.println(quizzes);
    		System.out.println(midterm);
    		System.out.println(finalExam);
    		System.out.println(totalScore);
    		System.out.println(letterGrade);
     
    	}
     
    	/**
    	 * Prompt the user to continue or quit
    	 * @return
    	 */
    	public static boolean  promptContinue()
    	{
    		// Put an actual method here; do an if-then, check for "Y" or "N", then return appropriate boolean
    		if()
    		return false;
     
    	}
    }

    I appreciate any help at all,
    -thanks much

    --- Update ---

    I get an error on the first line of the the display results and I tested some code (that I took out) for the boolean portion that hopefully I can get suggestions on. HOpe answers to this thread help other's as well


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Returning Boolean Values

    I get an error on the first line of the the display results
    It is important to realise that methods are "free standing". Your displayResults() method doesn't know anything about the student's name or id etc. All of this information has to be passed to the method so it can do the printing.

    public static String getLetterGrade(int totalScore)
    {
        if(totalScore >= 90 && totalScore <= 100)
        {
             etc

    Notice how you pass the value totalScore to the getLetterGrade() method so that it has the information to work on. It's the same thing with displayResults(): it's not going to return anything, but it does need all the values it is going to print.

    ---

    Those [Square brackets] shouldn't be your variables. They are just a quirk of how the pecifications are given.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    triumvirate1 (May 7th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 5 Times in 1 Post

    Default Re: Returning Boolean Values

    Thanks pbrockway2, that was something that I attempted to trial and error. I'll add code in and keep testing.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Returning Boolean Values

    You're welcome. Post back if you get stuck - unintelligible compiler messages, or whatever.

Similar Threads

  1. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 05:59 PM
  2. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 12th, 2012, 12:59 AM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM