Results 1 to 9 of 9
Thread: Questions on my code!
- 11-03-2010, 12:46 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Questions on my code!
1. Write a program that accepts the letter grades for a student, calculates the student's gpa, and prints it out, along with one of the following five messages:
Eligible
Ineligible, taking less than 4 classes
Ineligible, gpa below 2.0
Ineligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)
Ineligible, gpa below 2.0 and has F grade
2. Your program must use an appropriate sequence of nested if-else statements to print out the appropriate message.
3. The message "Ineligible, taking less than 4 classes" has priority over the other 3 ineligible cases.
4. The class will not ask the user for how many grades are in a student's report card. The program will continue to read grades until a non-grade character is input. At this point, some type of loop will cease and the program prints the GPA value and the eligibility message.
5. Example of run output: GPA = 3.75 Eligible
6. You do not have to print out any of the individual grades.
7. Your program should allow input of grades in either upper or lower case.
Java Code:import java.util.Scanner; public class Grades { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner grade= new Scanner(System.in); for (int count = 0; count < 30; count++) System.out.println("Enter letter grade: A, B, C, D, F"); String GradeValue = grade.nextLine(); { if (GradeValue.equals("A")); GPA = 4.0; } { if (GradeValue.equals("B")); GPA = 3.0; } { if (GradeValue.equals("C")); GPA = 2.0; } { if (GradeValue.equals("D")); GPA = 1.0; } { if (GradeValue.equals("F")); GPA = 0.0; } } }
- 11-03-2010, 01:06 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Does that code compile?
There's little point in moving forward if it has syntax errors. (If you can't understand the compiler's message copy and post the entire thing.)
Use standard Java naming conventions: gradeValue and it might pay to review your textbook for the syntax of basic structures. An if statement looks like this:
Java Code:String gradeValue = grade.nextLine(); if (gradeValue.equals("A")) { GPA = 4.0; }
- 11-03-2010, 01:14 AM #3
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
How do i have the scanner keep prompting me for grades.
Java Code:String another = "y"; while (another.equalsIgnoreCase("y")) { // code to do stuff System.out.println ("Enter y to Continue: "); another = scan.nextLine(); }
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 11-03-2010, 01:23 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
the code wont compile because of the GPA isnt declared as anythingg. should i put it as a double?>
- 11-03-2010, 01:37 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Yes - it has a decimal point, it's a double.
- 11-03-2010, 01:46 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Why when i run this code
Java Code:import java.util.Scanner; public class Grades { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner grade= new Scanner(System.in); for (int count = 0; count < 30; count++) System.out.println("Enter letter grade: A, B, C, D, F"); String GradeValue = grade.nextLine(); if (GradeValue.equals("A")); double GPA; { GPA = 4.0; } if (GradeValue.equals("B")); { GPA = 3.0; } if (GradeValue.equals("C")); { GPA = 2.0; } if (GradeValue.equals("D")); { GPA = 1.0; } if (GradeValue.equals("F")); { GPA = 0.0; } } }
- 11-03-2010, 02:56 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Can i just Ask, what degree is this assignment from?
- 11-03-2010, 03:26 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
it outputs the scanner like 30 times. i think because of the count?
You have a for loop that does something 30 times. But what is it supposed to do 30 times? Whatever it is, it should go between braces.
Java Code:for (int count = 0; count < 30; count++) { // all the code that has to be // done 30 times goes // here }
(A good practice, many find, is to always use braces even for one line blocks like your if statement. That way you can see at a glance being repeated.)
-----
@maknib: Sometimes it seems as if every Java course assigns this...
- 11-03-2010, 03:30 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
1. Seeing as you have this. "Ineligible, taking less than 4 classes" i would assume you need a counter to check if there was less than 4 entered.
2. im guessing you need if{} else if{}... else{} and else is then its not a Grade letter
3. this will need to be the first check in a sequence.
4. sounds like you need to run this while it's equal to a Grade
5. this is pretty straight foward. shows that you need a double var
6. good so we store them instead?
7. havn't seen this function myself but im guessing its something like string.toLower();
This is how i would look at this and tackle it but again i'm normally wrong :)
Cool! I just started a bachelor of technology in Computer studies and the first language is Java (also c, c++ c# php and others)
so i figured why not start early and learn some JAVA :) i already know Actionscript (bits of it) and php so i'm loving JAVA so far. quickly ecoming my fav language AND i've always wondered what my assignments might look like.. heads up in here :)Last edited by maknib; 11-03-2010 at 03:39 AM.
Similar Threads
-
Memory Leak questions, code review
By mensaFool in forum Advanced JavaReplies: 9Last Post: 03-08-2010, 05:27 PM -
UML, robust code, structure questions
By mac in forum New To JavaReplies: 2Last Post: 12-23-2009, 12:06 AM -
Some Questions
By MuslimCoder in forum New To JavaReplies: 2Last Post: 02-25-2009, 05:01 PM -
questions for 1yr exp
By rahaman.athiq in forum Java ServletReplies: 2Last Post: 11-26-2008, 02:13 AM -
Questions About JSP?
By mtz1406 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-19-2008, 08:56 PM
Bookmarks