Grading weight not calculating correctly - java

public class FinalMarkCalculator
{
/**
* This method is used to caculate the students mark
*/
public static void main(String[] args)
{
//Assigning the string a variable that holds the students name
String studentName;
//Initalizing a keyboard scanner
Scanner keyboardStudentName = new Scanner(System.in);
//System printing out the question
System.out.print("Enter the students name: ");
//Assigns the string studentName to the users input
studentName = keyboardStudentName.nextLine();
//Assigning the double a variable that hold the students assignment marks as a integer
double assignmentMarks;
//Assigning the string a variable that hold the students assignment marks
String assignmentMarksInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardAssignmentMarks = new Scanner(System.in);
//Asking the user for assingment marks while printing students name
System.out.printf("Enter %s's mark for Assignments (Max. 140): ", studentName);
//Taking the user input and assigning it to our string assignmentMarksInput
assignmentMarksInput = keyboardAssignmentMarks.nextLine();
//Converting the user input from a string to a double
assignmentMarks = Double.parseDouble(assignmentMarksInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double midTermExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String midTermExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardExamMidTermMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Mid-Term Exam (Max. 60): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
midTermExamMarkInput = keyboardExamMidTermMark.nextLine();
//Converting the user input from a string to a double
midTermExamMark = Double.parseDouble(midTermExamMarkInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double finalExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String finalExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardFinalExamMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Final Exam (Max. 85): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
finalExamMarkInput = keyboardFinalExamMark.nextLine();
//Converting the user input from a string to a double
finalExamMark = Double.parseDouble(finalExamMarkInput);
//Printing grade report title
System.out.println("Grade report" + "\n------------");
//Printing the students name
System.out.println(studentName + "\n");
//Printing the assignments mark
System.out.printf("%.2f/140 worth 15%%\n", assignmentMarks);
//Printing the Mid-Term exam mark
System.out.printf("%.2f/60 worth 40%%\n", midTermExamMark);
//Printing the Final exam mark
System.out.printf("%.2f/85 worth 45%%\n", finalExamMark);
double finalMark = (assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45);
//Printing the total calculated final mark
System.out.printf("\n\nFinal Mark: " + finalMark);
}
}
My code runs fine but it does not do the math correctly and I can't seem to figure out why even if I put everything perfect it still doesn't equal 100%.
This is the equation I'm using and I think this is how it should be done:
(assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45)

The maths should be
double finalMark = (assignmentMarks/140*15)+(midTermExamMark/60*40)+(finalExamMark/85*45);
Also I think it is better to not declare (and then later) initialize your variables - do in one step
double assignmentMarks = Double.parseDouble(assignmentMarksInput);
Also just use one Scanner
Scanner scanner = new Scanner(System.in);
and use it for all input
String studentName = scanner.nextLine();
....
String assignmentMarksInput = scanner.nextLine();

Related

Arrays With Multiple Information Per Box

So I have this practice question where I should create an array with the employee's information and pass it on to the class; there is a problem with my code which I cant seem to figure out.
What the code is meant to do is:
Have the information as seen in the code put into an array, then passed to the methods in the class and then printed out to the user. (The code in the class is perfectly fine, hence why it's not included here).
If anyone could help, that'd be awesome.
Thank you.
// Code.
// The Scanners.
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = input.nextInt();
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = input.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = input.nextInt();
E[i] = (name, bday, salary, overtime);
}
System.out.println("Employee's Information"
+ "\n----------------------"
+ "\n----------------------");
for(int i = 0; i <= E.length-1; i++){
E[i].print();
}
}
There are two issues here that need to be addressed.
First, don't create more than one Scanner object, reading from System.in. I know you did this, because of this issue (check it out): Scanner is skipping nextLine() after using next() or nextFoo()? - But there is another solution than creating another Scanner.
The second issue is here:
E[i] = (name, bday, salary, overtime);
You have a Employee[] which you are trying to fill. But you got the syntax wrong. You actually want to create a new Employee(...) to fill your array with.
So this line should correctly be (provided that Employee has a constructor with the given types):
E[i] = new Employee(name, bday, salary, overtime);
When considering this in your code snippet:
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = scan.nextInt();
scan.nextLine(); // <- reads the newline from the console
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = scan.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = scan.nextInt();
// create a new employee with the entered information and save in the array
E[i] = new Employee(name, bday, salary, overtime);
}
I am not sure with the Double scanner.
The obvious one is since you defined an empty array of Employee object type, the array can only be fit by Employee objects.
Employee[] E = new Employee[numberOfEmployees]
to define one object, you need to allocate a memory onto it, like:
new Employee(name, bday, salary, overtime);
then assign it into the array E
E[i] = new Employee(name, bday, salary, overtime);
i hope that clear things up.

JAVA using assigned values from an array in a calculation

This is a very basic question but I have just started out with JAVA and have hit a bit of a bump with regards to arrays.
What I am trying to do is populate an array with 6 pieces of information from the user:
Number of employees to be input,
An alphanumeric employee number,
A first name,
A last Name,
the number of hours they have worked,
a number input corresponding to Pay Scale.
So far I have gotten these inputs into an array in JAVA however what I wanted to do was use corresponding number input to select a constant within the Pay Scale array and then use that constant to calculate the wages of each employee.
for instance employee 1 worked 10 hours at scale 0 so that would be 10*4.50
and employee worked 10 hours at scale 1 which would be 10*4.62
import java.util.Arrays; //imports Array utility
import java.util.Scanner; //imports Scanner utility
public class test1 {
static Scanner keyb = new Scanner(System.in); //Adds a keyboard input
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Employees: ");
int employees = scanner.nextInt();
String[] Employee = new String[employees];
String[] FirstName = new String[employees];
String[] LastName = new String[employees];
double[] HoursWorked = new double[employees];
double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
for (int i = 0; i < Employee.length; i++) {
System.out.print("Enter Employee Number: ");
Employee[i] = scanner.next();
System.out.print("Enter Employee's First name: ");
FirstName[i] = scanner.next();
System.out.print("Enter Employee's Last name: ");
LastName[i] = scanner.next();
System.out.print("Enter Employee's Hours worked: ");
HoursWorked[i] = scanner.nextDouble();
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
}
for (int i = 0; i < HoursWorked.length; i++) {
System.out.println("Employee " + Employee[i] + " " + FirstName[i] + " " + LastName[i] + " has "
+ HoursWorked[i] * PayScale[0]);
}
}
}
}
Am I even close to a solution on this?
Is what I'm asking possible in JAVA?
Maybe I'm just looking at this the wrong way, but any help regarding this would be greatly appreciated.
edit
OK I added the extras array into the code
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Employees: ");
int employees = scanner.nextInt();
String[] Employee = new String[employees];
String[] FirstName = new String[employees];
String[] LastName = new String[employees];
double[] HoursWorked = new double[employees];
int[]PayScale2 = {0,1,2,3,4};
double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
I'm just unsure as to where I'd index the original PayScale array with the
PayScale[PayScale2[i]]
would it go into the for statement codeblock? (I have tried putting it in there however I get an error that it's not a statement :/
change
+ HoursWorked[i] * PayScale[0]);
to
+ HoursWorked[i] * PayScale[i]);
apart from that seems to me like you're doing what you're saying you should be doing..
you already have the payscales from here: double[] PayScale = {4.50,4.62,4.90,5.45,6.20}; so the following doesn't make a lot of sense:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
First of all, if you want to keep this number (Number 0 to 4) separately, you should use another Array, not the one where you keep the Payscales, then you could index to the first array which keeps the different rates.. or else you could directly use the first array if you know the pay scale for every employee.. in the end it has to do with what you want to do and how you want to do it, but the logic and the tools are there. If you call the 2nd array PayScale2 for example:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale2[i] = scanner.nextDouble();
then you can index to the first array for example:
PayScale[PayScale2[i]]
in which case if the user inputs 0 then PayScale2[i] would be 0 then PayScale[PayScale2[i]] would be PayScale[0] or 4.5 or whatever you set the value equal to at the first array

Scanner multiple inputs issue

I am a student at a local tech in Ireland first year. We are as you guessed doing java and the at the moment we are handling Scanners at the moment. I have added my code at the bottom and a little confused.
When I run the program it works fine till I get to the "Enter country" and the "Enter course" and these two line land up printing together no matter if I change the scanner to f.nextLine or f.next.
Anyone able to help me out with this as I am missing something and I can't figure out what it is.
Many thanks
Matthew
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"#student.ait.ie");
System.out.println("Please review the information.");
}
nextDouble() scans the next token and parses it into a double. That is why you are facing this issue. You can simply move you nextLine() above the nextDouble(). Its a temporary solution to solve the problem.
Try below code. I just edited your code.
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"#student.ait.ie");
System.out.println("Please review the information.");
}
}
If you use
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.next();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.next();
it will work. You can do this for the other nextLines also. For some reason you read the student number as a string instead of a number, but since you treat all you input as strings, you could just use next for everything.
Edit:
Don't forget to call f.close() to close the scanner.

Java array to input student names according to their number

I need to create an array that prompts a professor to input how many students are in their class. Then prompts them to input their names until the number of students is met. What I have is clearly wrong but I was hoping for some insight.
System.out.println("Please enter the number of students in the class: ");
int numberOfStudents = console.nextInt();
String [] studentName = new String [numberOfStudents];
for (int i=0; i<studentName.length; i++)
{
System.out.println("Enter the name of student " + (i+1) + " in your class. ");
studentName[i] = console.nextLine();
System.out.println("Student name entered: " + studentName[i]);
}
EDIT: I changed the code a bit, mainly the array. With the for loop I am intending to simply have it go through each number and assign a student name to it. But with the last line of code it gives me an error saying its a confusing indentation.
EDIT 2: After proofreading my question and the code myself I've noticed very basic mistakes and have dealt with them, but one last question. Right now while the code works, when it asks for me to input the name, it skips student 1, leaves it blank then moves onto student 2. As shown in this screenshot http://puu.sh/8fl8e.png
you weren't far...
Scanner console = new Scanner (System.in);
System.out.println("Please enter the number of students in the class: ");
int numberOfStudents = console.nextInt();
String [] studentName = new String [numberOfStudents];
for (int i=0; i<studentName.length; i++){
System.out.println("Enter the name of student" + (i+1) + "in your class. ");
studentName[i] = console.next();
}
to test the code:
for (int i=0; i<studentName.length; i++){
System.out.println(studentName[i]);
}
EDIT: an answer for your second edit:
use
console.next();
instead of
console.nextLine();
I guess that studentName should be String, not double. And maybe you should consider taking advantage of fact that Java is OO? :)
since you are taking names as input which is string type but you are using double(not possible to store string) and you also missed bracket after for loop.
change
double [] studentName = new double [numberOfStudents];
to
String [] studentName = new String [numberOfStudents];
final correct code:
System.out.println("Please enter the number of students in the class: ");
int numberOfStudents = console.nextInt();
String [] studentName = new String[numberOfStudents];
for (int i=0; i< numberOfStudents; i++){
System.out.println("Enter the name of student" + (i+1) + "in your class. ");
studentName[i] = console.nextLine();
}
System.out.println("Please enter the number of students in the class: ");
int numberOfStudents = console.nextInt();
String [] studentName = new String [numberOfStudents];
for (int i=0; i<studentName.length; i++)
{
System.out.println("Enter the name of student " + (i+1) + " in your class. ");
studentName[i] = console.nextLine();
System.out.println("Student name entered: " + studentName[i]);
}

"error: variable keyboard is already defined in method main(String [])"

I'm getting several error messages when I try to run my program, the main one which bothers me being "error: variable keyboard is already defined in method main(String [])"
Am I supposed to but main(String []) more than once in my program, or just in the beginning as I have it? What else could be wrong here?
Here is the beginning of my program:
public static void main(String[]args)
{
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard = new Scanner(System.in);
firstName = keyboard.nextString();
System.out.println("Enter your last name:");
Scanner keyboard = new Scanner(System.in);
lastName = keyboard.nextString();
System.out.println("Enter the number of movies downloaded:");
Scanner keyboard = new Scanner(System.in);
moviesDownloaded = keyboard.nextInt();
System.out.println("Enter the cost per movie:");
Scanner keyboard = new Scanner(System.in);
movieCost = keyboard.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
Scanner keyboard = new Scanner(System.in);
stateResidency = keyboard.nextInt();
You should only declare and initialize keyboard once and then use it. So remove all lines of the type: Scanner keyboard = new Scanner(System.in); apart from the first one.
Otherwise you try to declare the same variable multiple times and thus java complains.
I'm guessing this has been long solved by this point but I came across it and I really like closure, because Ivaylo Strandjev answer was not chosen. Also in case anyone else stumbles across this.
The error is saying you defined the variable keyboard in this scope already and you are trying to define it again.
like Ivaylo Strandjev was saying.
You can try the following:
1 remove the declaration portion
public static void main(String[]args) {
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard = new Scanner(System.in);
firstName = keyboard.nextString();
System.out.println("Enter your last name:");
keyboard = new Scanner(System.in); //-----changed
lastName = keyboard.nextString();
System.out.println("Enter the number of movies downloaded:");
keyboard = new Scanner(System.in); //-----changed
moviesDownloaded = keyboard.nextInt();
System.out.println("Enter the cost per movie:");
keyboard = new Scanner(System.in);//-----changed
movieCost = keyboard.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
keyboard = new Scanner(System.in);//-----changed
stateResidency = keyboard.nextInt();`
or this, change the new variable names
public static void main(String[]args){
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard1 = new Scanner(System.in);
firstName = keyboard1.nextString();
System.out.println("Enter your last name:");
Scanner keyboard2 = new Scanner(System.in);//-----changed
lastName = keyboard2.nextString();
System.out.println("Enter the number of movies downloaded:");
Scanner keyboard3 = new Scanner(System.in);//-----changed
moviesDownloaded = keyboard3.nextInt();
System.out.println("Enter the cost per movie:");
Scanner keyboard4 = new Scanner(System.in);//-----changed
movieCost = keyboard4.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
Scanner keyboard5 = new Scanner(System.in);//-----changed
stateResidency = keyboard5.nextInt();`
You don't need to initialize Keyboard every time you use it. You can declare it once at the top of the program, and just call keyboard.next() each time you want to get something from it.

Categories

Resources