Here's the superclass:
public class MemoryCalc {
private double currentValue;
public double getCurrentValue() {
return currentValue;
}
public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;
}
public int displayMenu() {
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int choice = -1;
while (choice < 1 || choice > 6) {
System.out.println();
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Clear");
System.out.println("6. Quit");
System.out.println();
System.out.print("What would you like to do? ");
choice = input.nextInt();
if (choice < 1 || choice > 6) {
System.out.println(choice + " wasn't one of the options");
}
if (choice == 6) {
System.out.println("Goodbye!");
System.exit(0);
}
}
return choice;
}
public double getOperand(String prompt) {
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print(prompt);
return input.nextDouble();
}
public void add(double op2) {
currentValue += op2;
}
public void subtract(double op2) {
currentValue -= op2;
}
public void multiply(double op2) {
currentValue *= op2;
}
public void divide(double op2) {
if (op2 == 0) {
currentValue = Double.NaN;
} else {
currentValue /= op2;
}
}
public void clear() {
currentValue = 0;
}
}
Here's the Subclass:
class SciMemCalc extends MemoryCalc{
private double currentValue;
public int displayMenu(){
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int choice = -1;
while (choice < 1|| choice > 8){
System.out.println();
System.out.println("Menu:");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Power");
System.out.println("6. Logarithm");
System.out.println("7. Clear");
System.out.println("8. Quit");
System.out.println();
System.out.println("What would you like to do?");
choice = input.nextInt();
if (choice < 1|| choice > 8){
System.out.println(choice +" wasn't one of the options");
}
if (choice == 8){
System.out.println("Thank you, good bye!");
System.exit(0);
}
}
return choice;
}
public void power(double op2){
Math.pow(currentValue, op2);
}
public void log() {
Math.log(currentValue);
}
}
Here's the Driver:
public class SciCalcDriver {
public static void main(String[] args) {
SciMemCalc calc = new SciMemCalc();
while (true){
System.out.println("The current value is " + calc.getCurrentValue());
int choice = calc.displayMenu();
double second = 0;
if (choice < 6) {
second = calc.getOperand("What is the second number? ");
}
if (choice == 1) {
calc.add(second);
} else if (choice == 2){
calc.subtract(second);
} else if (choice == 3){
calc.multiply(second);
} else if (choice == 4){
calc.divide(second);
} else if (choice == 5){
calc.power(second);
} else if (choice == 6){
calc.log();
} else if (choice == 7){
calc.clear();
}
}
}
}
Now the calculator can add, subtract, multiply, and divide fine but when I use the power or log method nothing happens. I've tried using the debugger and it says it get's all the necessary inputs, but the currentValue doesn't seem to change. I don't think I need to make any changes to the superclass. Advice?
class MemoryCalc {
private double currentValue;
}
class SciMemCalc extends MemoryCalc {
private double currentValue;
}
What is happening here is there are actually two variables declared with the same name. SciMemCalc does not have access to the variable declared in MemoryCalc because it is private.
Instead you would normally make currentValue protected or interact with it through setters and getters.
class MemoryCalc {
protected double currentValue;
}
class SciMemCalc extends MemoryCalc {
// SciMemCalc has access to currentValue
}
Remove private double currentValue; from the subclass and change private double currentValue; in the superclass to protected double currentValue;. If you have a private value, it can't be accessed by the superclass, and the duplicate value you have created just hides the original. Protected solves this.
You're shadowing your superclass' currentValue by re-declaring it in your subclass. To resolve the issue, in your subclass SciMemCalc, get rid of the line declaring currentValue:
private double currentValue;
And change all of SciMemCalc's accesses to currentValue to use the superclass' accessor (getter) and mutator (setter) methods:
public void power(double op2){
setCurrentValue(Math.pow(getCurrentValue(), op2));
}
public void log() {
setCurrentValue(Math.log(getCurrentValue()));
}
Alternatively, you could declare currentValue as protected in your superclass which would allow you to access it directly in your subclass. However, also in this case, you have to make sure that you assign the result of your calculation back to currentValue:
public void power(double op2){
currentValue = Math.pow(currentValue, op2);
}
public void log() {
currentValue = Math.log(currentValue);
}
You have declared the currentValue as a private member of class so it cannot be accessed by other
classes. when you make it protected or public then it will work. And will solve you problem.
just change that currentValue field in super class.
protected double currentValue // it will be only accessed by its child class
public double currentValue // it will be accessed by all classes in this package
Related
I want to do a simple beginner project using methods, if statements, and user input. I am having an issue though with the calc() method. How can I return two different data types in java, and if I cannot, how could I do it, still by using more than the main method?
import java.util.Scanner; //allow user input
public class fourFunctionCalculator{
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Enter your first number:"); //get first number
double num1 = keyboardInput.nextDouble();
System.out.print("Enter your operator: "); // get operator
String name = keyboardInput.next(); //grabs everything user types until a space
System.out.print("Enter your second number: "); //get second number
double num2 = keyboardInput.nextDouble();
System.out.println(calc(num1,op,num2));
}
//troublesome part is here
public static double calc(double num1, String op, double num2){
if (op == "+") {
return (num1 + num2);
}
else if (op == "-") {
return (num1 - num2);
}
else if (op == "*") {
return (num1 * num2);
}
else if (op == "/") {
return (num1 / num2);
}
else {
return ("INVALID OPERATOR");
}
}
}
you could generate a custom Exception, also you need to use the method .equals() inside the if validations, otherwise it is not going to work.
fourFunctionCalculator.java
import java.util.Scanner; //allow user input
public class fourFunctionCalculator{
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Enter your first number:"); //get first number
double num1 = keyboardInput.nextDouble();
System.out.print("Enter your operator: "); // get operator
String name = keyboardInput.next(); //grabs everything user types until a space
System.out.print("Enter your second number: "); //get second number
double num2 = keyboardInput.nextDouble();
try {
System.out.println(calc(num1,name,num2));
} catch (InvalidOperatorException e) {
System.out.println(e);
}
}
public static double calc(double num1, String op, double num2){
if (op.equals("+")) {
return (num1 + num2);
}
else if (op.equals("-")) {
return (num1 - num2);
}
else if (op.equals("*")) {
return (num1 * num2);
}
else if (op.equals("/")) {enter code here
return (num1 / num2);
}
throw new InvalidOperatorException("INVALID OPERATOR : " + op);
}
}
InvalidOperatorException.java
public class InvalidOperatorException
extends RuntimeException {
private static final long serialVersionUID = 1L;
public InvalidOperatorException(String errorMessage) {
super(errorMessage);
}
}
I recommend returning an OptionalDouble object as placeholder of something valid or not...
Ex #1: return OptionalDouble.of(num1 + num2); // first if outcome
Ex #2: return OptionalDouble.empty(); // for the last else
Then your main(...) method needs to be something like...
System.out.println(calc(num1,op,num2).orElseThrow(() -> new IllegalArgumentException("INVALID OPERATOR")));
I suggest returning always String.
public static String calc(double num1, String op, double num2){
if (op == "+") {
return String.valueOf(num1 + num2);
}
else if (op == "-") {
return String.valueOf(num1 - num2);
}
else if (op == "*") {
return String.valueOf(num1 * num2);
}
else if (op == "/") {
return String.valueOf(num1 / num2);
}
else {
return ("INVALID OPERATOR");
}
}
I designed a small program where I have three exam grades and I use the Grades class to compute the average of the three. Also, I prompt for the exam number (1,2, or 3) and it should return it. However, I keep getting 0.0 as the output for both the average exam score and chosen exam score.
package GradesClass;
import java.util.Scanner;
public class GradesDriver {
public static void main(String[] args) {
Grades school = new Grades(90.9,87.9,99.9);
Scanner in = new Scanner(System.in);
System.out.println("Enter desired test number: ");
int testnumber = in.nextInt();
System.out.println(school);
System.out.println("Exam score: " + school.getGrades(testnumber));
}
}
package GradesClass;
public class Grades {
private double num1, num2, num3;
private int testnumber;
private double average;
public Grades(double num1, double num2, double num3) {
num1 = 0;
num2 = 0;
num3 = 0;
}
public void setGrades(double scorenumber, int testnumber) {
if (testnumber == 1) {
num1 = scorenumber;
} else if (testnumber == 2) {
num2 = scorenumber;
} else {
num3 = scorenumber;
}
}
public double getGrades(int testnumber) {
if (testnumber == 1) {
return(num1);
} else if (testnumber == 2) {
return(num2);
} else {
return(num3);
}
}
public double average(double num1, double num2, double num3) {
average = ((num1+num2+num3)/3.0);
return(average);
}
public String toString() {
return("Average: " + average);
}
}
In your constructor for Grades you are setting the member variables to zero instead of the values supplied in the parameters. Change the constructor to
public Grades(double num1, double num2, double num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
I have an assignment that was to make a subclass which adds on to the existing super class, and this is run by a third class with a main method which creates an object of the subclass and calls appropriate methods.
My problem is that calling methods in the super class works correctly, but calling the two additional methods I have written in the subclass gets ignored.
I've checked, and the main method IS getting to the if statement which calls the new method in the subclass, but this method is not executing.
Specifically the superclass is a memory calculator with basic add, subtract, divide, and multiply options, as well as an option to clear the current value.
The subclass is a scientific memory calculator that overrides the menu method in the superclass to include options for power and logarithm, and has methods for both.
When I choose the option for power, it is simply returning the current value and not raising that power to the number specified.
Superclass:
import java.util.Scanner;
public class MemoryCalc {
private double currentValue = 0;
public static int displayMenu() {
Scanner input = new Scanner(System.in);
System.out.print("Menu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Clear\n6. Quit\nWhat would you like to do? ");
int menuChoice = input.nextInt();
while (menuChoice < 1 || menuChoice > 6) {
System.out.print("Please enter a valid option.\n\n");
System.out.print("Menu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Clear\n6. Quit\nWhat would you like to do? ");
menuChoice = input.nextInt();
}
return menuChoice;
}
public static double getOperand(String prompt) {
Scanner input = new Scanner(System.in);
System.out.print(prompt);
double operand = input.nextDouble();
return operand;
}
public double getCurrentValue() {
return currentValue;
}
public void add(double operand2) {
currentValue = currentValue + operand2;
}
public void subtract(double operand2) {
currentValue = currentValue - operand2;
}
public void multiply(double operand2) {
currentValue = currentValue * operand2;
}
public void divide(double operand2) {
if (operand2 == 0) {
currentValue = Double.NaN;
}
else currentValue = currentValue / operand2;
}
public void clear() {
currentValue = 0;
}
}
Subclass:
import java.util.Scanner;
public final class ScientificMemCalc extends MemoryCalc {
private double currentValue = 0;
public static int displayMenu() {
Scanner input = new Scanner(System.in);
int menuChoice = -1;
while (menuChoice < 1 || menuChoice > 8) {
System.out.println();
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Power");
System.out.println("6. Logarithm");
System.out.println("7. Clear");
System.out.println("8. Quit");
System.out.println();
System.out.print("What would you like to do? ");
menuChoice = input.nextInt();
if (menuChoice < 1 || menuChoice > 8) {
System.out.println(menuChoice + " wasn't one of the options");
}
}
return menuChoice;
}
public void power(double operand2) {
currentValue = Math.pow(currentValue, operand2);
}
public void logarithm() {
currentValue = Math.log(currentValue);
}
}
Main Method:
public class ScientificCalcDriver {
public static void main(String[] args) {
ScientificMemCalc calculator1 = new ScientificMemCalc();
int menuChoice = 0;
while (menuChoice != 8) {
System.out.print("The current value is " + calculator1.getCurrentValue() + "\n");
menuChoice = ScientificMemCalc.displayMenu();
if (menuChoice < 6) {
double Operand2 = ScientificMemCalc.getOperand("What is the second number? ");
if (menuChoice == 1) {
calculator1.add(Operand2);
}
if (menuChoice == 2) {
calculator1.subtract(Operand2);
}
if (menuChoice == 3) {
calculator1.multiply(Operand2);
}
if (menuChoice == 4) {
calculator1.divide(Operand2);
}
if (menuChoice == 5) {
calculator1.power(Operand2);
}
}
else if (menuChoice == 6) {
calculator1.logarithm();
}
else if (menuChoice == 7) {
calculator1.clear();
}
else if (menuChoice == 8) {
System.out.print("Goodbye!");
System.exit(0);
}
}
}
}
You've got two different currentValue variables, both private, one in the superclass and one in the subclass. The methods in your superclass are changing the value of its variable, and the methods of the subclass are changing the value of its variable.
Make currentValue protected in the superclass, and remove it from the subclass.
Update:
Based on the "must be private" comment: You still need to remove the extra currentValue variable in the subclass, but as Oleg points out, you can obtain and update the value of a private variable in a superclass using get and set methods defined in the superclass.
I wrote this program to convert a number grade into a letter grade but I keep getting the same errors. Could someone help me figure out what I'm doing wrong?
import static java.lang.System.*;
import java.util.Scanner;
public class Grade
{
private int numGrade;
public Grade()
{
Grade test;
}
public void setGrade(int grade)
{
numGrade = grade;
if (grade >= 90)
{
System.out.println("A");
}
{
System.out.println("B");
}
public String getLetterGrade( ) {
String letGrade="A";
if (grade>= 90)
{
return letGrade;
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
Seems like you tried to attack the same thing from many different positions.
first of lets start with converting numerical grades into letter grades, so before engaging to inputs, start with asking the kind of grade the user wishes to convert
char choise;
choise = reader.nextChar(); //ask for N or L for numerical or letter
next i'll show a sample code for letter to numerical convertion
public int getNGrade(char grade)
{
if (grade == 'A')
return 90;
else if (grade == 'B')
return 80; //and so on
}
same way can be used for the numerical to letter convertion
in the main class u call the function:
charGrade = reader.nextChar();
System.out.println("Your grade in numbers is " + getNGrade(charGrade));
i'm guessing that's what u meant, hope i was helpful.
Your code can be this and it works perfectly:
public class Grade {
private int numGrade;
public Grade(int grade) {
numGrade = grade;
}
public int getGrade() {
return numGrade;
}
public void setGrade(int grade) {
numGrade = grade;
}
public String getLetterGrade() {
if(numGrade <0 || numGrade > 100) throw new IllegalArgumentException("No such a grade!");
else if(numGrade>=90) return "A";
else if (numGrade >= 80) return "B";
else if(numGrade >= 70) return "C";
else if(numGrade >= 60) return "D";
else return "F";
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
You can include in the same class a main method or create a separate class for testing:
public static void main(String[] args) {
Grade g = new Grade(75); //you can enter the grade manually or simply using a Scanner object
System.out.println(g);
}
The syntax used is incorrect.
Attached is a sample code to do the conversion
public class Grade {
private int numGrade;
public void setGrade(int grade) {
numGrade = grade;
if (grade >= 90) {
System.out.println("A");
} else {
System.out.println("B");
}
}
public String getLetterGrade() {
String letGrade = "B";
if (numGrade >= 90) {
return "A";
}
return letGrade;
}
public String toString() {
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
import java.util.Scanner;
class Tutorial {
public static void main(String args[]){
Scanner input = new Scanner(System.in); // calling Scanner method
String restart = "Y"; //initialising the restart variable
while (restart.equals("Y")) // testing the conditon(if Y is equals then it continues)
{
int grade;
System.out.println("WELCOME TO ABD GRADING SYSTEM.");
System.out.println("Enter your Score(between 1 - 100) : "); // Displaying a message on screen
grade = input.nextInt(); // Accept Input from the user
if(grade<=39)
System.out.println("Your grade is F9");
else if(grade==40 || grade<=49)
System.out.println("Your grade is D7");
else if(grade==50 || grade<=59)
System.out.println("Your grade is C6");
else if(grade==60 || grade<=69)
System.out.println("Your grade is C5");
else if(grade==70 || grade<=79)
System.out.println("Your grade is B2");
else if(grade==80 || grade<=100)
System.out.println("Your grade is A1");
else
{
System.out.println("Input Correct score between (1 - 100).");
}
System.out.println("THANK YOU.");
System.out.println("Would you like to Calculate again? Y/N ");
restart = input.next();
}
}
}
I have this program that asks user to enter a value and it calculates it as the user like to initial value of zero, then it ask the user what process to do again and ask the user to enter a value again and it calculates it to the last value of the instance, the problem is every time it asks the user to enter value it calculates it to zero not to the last entry. Please help me find the bug:
The program has to has two classes, one for the calculator and the other for the methods:
FIRST CLASS
public class MainClass {
public static void main(String[] args) {
MemoryCalculator calc = new MemoryCalculator();
calc.getCurrentValue();
displayMenu();
}
public static int displayMenu() {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("Menu");
System.out.println("1.Add");
System.out.println("2.Subtract");
System.out.println("3.Multiply");
System.out.println("4.Divide");
System.out.println("5.Clear");
System.out.println("6.Quit");
System.out.println("");
System.out.println("What would you like to do?");
choice = input.nextInt();
if (choice > 6 || choice < 1) {
System.out.println("Sorry," + choice + " was not an option");
return displayMenu();
}
} while (choice > 6 || choice < 1);
MemoryCalculator calc = new MemoryCalculator();
if (choice == 5) {
calc.clear();
return 0;
} else if (choice == 6) {
System.out.println("Goodbye! ");
System.exit(0);
}
System.out.println("What is the second number? ");
double operand2 = input.nextDouble();
switch (choice) {
case 1:
calc.add(operand2);
break;
case 2:
calc.subtract(operand2);
break;
case 3:
calc.multiply(operand2);
break;
case 4:
calc.divide(operand2);
break;
}
return displayMenu();
}
public static double getOperand(String prompt) {
return 0;
}
}
SECOND CLASS
public class MemoryCalculator {
private double currentValue;
public double getCurrentValue() {
System.out.println("The current value is " + currentValue);
return 0;
}
public void add(double operand2) {
currentValue = currentValue + operand2;
getCurrentValue();
}
public void subtract(double operand2) {
currentValue -= operand2;
getCurrentValue();
}
public void multiply(double operand2) {
currentValue *= operand2;
getCurrentValue();
}
public void divide(double operand2) {
if (operand2 == 0) {
System.out.println("Sorry, you can not divide by 0");
}
currentValue /= operand2;
getCurrentValue();
}
public void clear() {
currentValue = 0;
getCurrentValue();
}
}
You probably want to keep the last value stored in "calc". I see 3 bugs.
Move this line before the start of your "do" loop. This will keep it from reseting the value inside this variable/class.
MemoryCalculator calc = new MemoryCalculator();
Move your ending "while" loop line to the bottom of your method(right before the return statement). It only appears to be working because in your return statement you are calling your method again...see #3. Also you will want to change the "or" to the "and" operator in the while statement "choice>6 && choice<1"
}while(choice>6 && choice<1);
In you displayMenu method change the return statement, because you don't want it to call itself in an infinite loop... now that the do while loop is fixed.
return displayMenu();
to this
return choice;