I entered in this code for my programming fundamentals class for this question
To encourage good grades, Hermosa High School has decided to award each student a bookstore credit that is 10 times the student’s grade point average.
In other words, a student with a 3.2 grade point average receives a $32.0 credit.
Create an application that prompts a student for a name and grade point average, and then passes the values to a method (computeDiscount) that displays a descriptive message. The message uses the student’s name, echoes the grade point average, and computes and displays the credit, as follows: John, your GPA is 3.4, so your credit is $34.0.
I am only 83% done and need to figure out blake 1.21 and kate 3.87, what else do i need to add or get rid of?
import java.util.Scanner;
public class BookstoreCredit
{
public static void main(String[] args)
{
Scanner abc = new Scanner(System.in); //declaring scanner to take input from user
System.out.println("Enter your name ");
String name=abc.nextLine(); //taking input from user
System.out.println("Enter your GPA ");
Float gpa=abc.nextFloat(); //taking input from user
computeDiscount(name,gpa); //calling the function
}
private static void computeDiscount(String stdName,double gradeAverage)
{
Double gpa=gradeAverage; //copying the value of gradeAverage in gpa
gradeAverage = gradeAverage*10;
int i=(int)gradeAverage; //typecasting gradeAverage to int
System.out.println(" " + stdName +" ,your GPA is " + String.format("%.1f", gpa) +" ,so your credit is $"+ i );
}
}
Sol:
Changing last two lines will do the job.
First- Typecast gradeAverage to float not int. Type below line in place of second last line:
float i=(float)gradeAverage; //typecasting gradeAverage to float
Second- GPA is formatted to 1 float point instead of that you should change it to 2 floating pointing inorder to display GPA properly. Swap the original last line of code with below line:
System.out.println(" " + stdName +" ,your GPA is " +
String.format("%.2f", gpa) +" ,so your credit is $"+ i );
Get Answers For Free
Most questions answered within 1 hours.