Code Help!
-
I cannot figure out what I am doing wrong. I just don't feel like I'm ever going to understand all of this. My prompts and loop are working fine. However, I am not getting any output. I'm sure it's a fairly simple thing, but I just don't see it so I can't know for sure what I need to fix. I have watched all the videos and looked through my textbook and the internet, but I need someone to "dumb" it down for me, I guess. Thank you, in advance, for your patience and help.
import java.util.Scanner;
public class StudentInfoTest
{
public static void main(String[] args)
{
//Initiate scanner.
Scanner input = new Scanner(System.in);//Declare Variables. String answer = ""; double GPA = 0.0; //Create Object StudentInfo info = new StudentInfo(null, 0, 0); //Prompt for and store student name. System.out.print("Enter student name: "); String name = input.nextLine(); info.setName(name); //Prompt for class credits and letter grade. //Loop until answer is "N". do { System.out.print("Enter class credits: "); String credits = input.nextLine(); System.out.print("Enter letter grade: "); String grade = input.nextLine(); System.out.print("Add another class? (Y or N): "); answer = input.nextLine(); } while (answer.equals("Y")); System.out.printf(name + ", Your GPA is %.2f/n.", GPA); }//Ends Main
}//Ends Class
public class StudentInfo {
//Variables private String studentName; private double totalPoints; private double totalCredits; //Constructor public StudentInfo(String name, double points, double credits) { //Initialize Variables studentName = name; totalPoints = points; totalCredits = credits; } //Create "Set" Functions public void setName(String name) { studentName = name; } //Create "Get" Functions public String getName() { return studentName; } public void addClass(double credits, String grade) { double points = 0.0; if(grade == "A" | grade == "a") points = 4.0; if(grade == "B" | grade == "b") points = 3.0; if(grade == "C" | grade == "c&quo
-
I cannot figure out what I am doing wrong. I just don't feel like I'm ever going to understand all of this. My prompts and loop are working fine. However, I am not getting any output. I'm sure it's a fairly simple thing, but I just don't see it so I can't know for sure what I need to fix. I have watched all the videos and looked through my textbook and the internet, but I need someone to "dumb" it down for me, I guess. Thank you, in advance, for your patience and help.
import java.util.Scanner;
public class StudentInfoTest
{
public static void main(String[] args)
{
//Initiate scanner.
Scanner input = new Scanner(System.in);//Declare Variables. String answer = ""; double GPA = 0.0; //Create Object StudentInfo info = new StudentInfo(null, 0, 0); //Prompt for and store student name. System.out.print("Enter student name: "); String name = input.nextLine(); info.setName(name); //Prompt for class credits and letter grade. //Loop until answer is "N". do { System.out.print("Enter class credits: "); String credits = input.nextLine(); System.out.print("Enter letter grade: "); String grade = input.nextLine(); System.out.print("Add another class? (Y or N): "); answer = input.nextLine(); } while (answer.equals("Y")); System.out.printf(name + ", Your GPA is %.2f/n.", GPA); }//Ends Main
}//Ends Class
public class StudentInfo {
//Variables private String studentName; private double totalPoints; private double totalCredits; //Constructor public StudentInfo(String name, double points, double credits) { //Initialize Variables studentName = name; totalPoints = points; totalCredits = credits; } //Create "Set" Functions public void setName(String name) { studentName = name; } //Create "Get" Functions public String getName() { return studentName; } public void addClass(double credits, String grade) { double points = 0.0; if(grade == "A" | grade == "a") points = 4.0; if(grade == "B" | grade == "b") points = 3.0; if(grade == "C" | grade == "c&quo
If I understand your question correctly, you never get the correct GPA? If that is the case, it looks like you never call
getGPA
, you only use the initial (0.0) value. So instead ofSystem.out.printf(name + ", Your GPA is %.2f/n.", GPA);
Should you have
System.out.printf(name + ", Your GPA is %.2f/n.", info.getGPA());
The same applies to credits and grade. You never seem to call
addClass
-
If I understand your question correctly, you never get the correct GPA? If that is the case, it looks like you never call
getGPA
, you only use the initial (0.0) value. So instead ofSystem.out.printf(name + ", Your GPA is %.2f/n.", GPA);
Should you have
System.out.printf(name + ", Your GPA is %.2f/n.", info.getGPA());
The same applies to credits and grade. You never seem to call
addClass
I guess I don't understand what I am supposed to do to call them...
-
I guess I don't understand what I am supposed to do to call them...
Have a look at the example I wrote. The same way, after you have got the input from the user call the addClass method with your variables... Something like
info.addClass(credits, grade);
Just keep in mind that the next thing you should implement is proper conversion between the data types (from string to double)
-
Have a look at the example I wrote. The same way, after you have got the input from the user call the addClass method with your variables... Something like
info.addClass(credits, grade);
Just keep in mind that the next thing you should implement is proper conversion between the data types (from string to double)
I really appreciate your help... Apparently, I'm EXTREMELY dumb with this. So you mean... That I have:
System.out.print("Enter class credits: ");
String credits = input.nextLine();And I need to change it to:
System.out.print("Enter class credits: ");
double credits = input.nextDouble();Where would I put the:
info.addClass(credits, grade);
When I added it after the grade code, it messed up my prompts.
-
I guess I don't understand what I am supposed to do to call them...