I have written the following java code where I am getting 0 for area of A triangle. What can be the reason for this erroneous output?
package methodoverrideoverload;
import java.io.*;
import java.util.*;
class Figure_Area {
int dima;
int dimb;
Figure_Area()
{
}
Figure_Area(int i, int j)
{
dima=i;
dimb=j;
}
void cal_area()
{
System.out.println("Area is undefined!!");
}
}
class Figure_Area_Tri extends Figure_Area {
Figure_Area_Tri(int i, int j)
{
super(i,j);
}
void cal_area()
{
System.out.println("The area of the Triangle is "+((1/2)*dima*dimb));
}
}
class Figure_Area_Rec extends Figure_Area {
Figure_Area_Rec(int i, int j)
{
super(i,j);
}
void cal_area()
{
System.out.println("The area of the Rectangle is "+ dima*dimb);
}
}
class Figure_Area_Cir extends Figure_Area {
double pi=3.14;
Figure_Area_Cir(int i)
{
super.dima=i;
}
void cal_area()
{
System.out.println("The area of the Circle is "+ (pi*dima*dima));
}
}
class findArea {
public static void main(String args[]) throws IOException
{
System.out.println("<<-MENU-->>");
System.out.println("Enter the figure you want to know the area of->");
System.out.println("Press 1 for Triangle");
System.out.println("Press 2 for Rectangle");
System.out.println("Press 3 for Circle");
System.out.println("Press 4 to Exit");
System.out.println("Enter your choice->");
Scanner sc=new Scanner(System.in);
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Height->");
int h=sc.nextInt();
System.out.println("Enter Base->");
int b=sc.nextInt();
Figure_Area_Tri tri=new Figure_Area_Tri(h,b);
tri.cal_area();
break;
case 2:
System.out.println("Enter Length->");
int len=sc.nextInt();
System.out.println("Enter Breadth->");
int bre=sc.nextInt();
Figure_Area_Rec rec=new Figure_Area_Rec(len,bre);
rec.cal_area();
break;
case 3:
System.out.println("Enter Radius->");
int rad=sc.nextInt();
Figure_Area_Cir cir=new Figure_Area_Cir(rad);
cir.cal_area();
break;
case 4:
break;
default:
System.out.println("Sorry! Invalid Choice");
}
}
}
I have examined the code in debugging mode and found out the values of dima and dimb are set as the input value, but somehow the function for area is most likely unable to give the correct result.
With integers variables java uses integer maths, for example
1/2==0
To solve this use double maths
1.0/2==0.5
1d would also work instead of 1.0
So in your case
(1.0/2)*dima*dimb
As long as one of the variables is a double then double maths applies (be careful when the equation contains multiple "parts", each part must have a double in it).
What won't help and why
Note just casting to double ((double)(1/2)*dima*dimb) won't solve the problem because the detail is already lost by the point of the cast to double, it goes as
(double)(1/2)*dima*dimb
(double)(0)*(dima*dimb)
(double)(0)
0.0
You are using 1/2 which equals 0
You need to use a double, so 1.0 / 2.0 = 0.5
Try using double or float for 1/2.
If you use integers it will be 0 as there is no such thing as 0.5 with integers.
You can write 1/2f for example or 1/2d or look at other answers :)
Related
I was creating a calculator. And in it, I have included different methods for different operations. It will take 2 numbers from the user and then ask for what operation to perform, and then execute the corresponding method. Here's the program:
import java.io.*;
public class calculator
{
void add(double a,double b)
{
double a1=a+b;
System.out.println("Their sum will be "+a1);
}
void subtract(double a,double b)
{
double a2=a-b;
System.out.println("Their difference will be "+a2);
}
void multiply(double a,double b)
{
double a3=a*b;
System.out.println("Their product will be "+a3);
}
void divide(double a,double b)
{
double a4=a/b;
System.out.println("Their division will result to "+a4);
}
void findremainder(double a,double b)
{
if (a>b)
{
double a5a=a%b;
System.out.println("The remainder obtained after their division is "+a5a);
}
if (b>a)
{
double a5b=b%a;
System.out.println("The remainder obtained after their division is "+a5b);
}
}
void percent(double a,double b)throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("What type of percentage you want ? [give the number in the list below which correspons to that operation]");
System.out.println("1=> Smaller number by Bigger number");
System.out.println("2=> Bigger number by smaller number");
int o=Integer.parseInt(in.readLine());
if (o==1)
{
if (a>b)
{
double per=(100*b)/a;
System.out.println("The percentage is "+per+"%");
}
if (b>a)
{
double per2=(100*a)/b;
System.out.println("The percentage is "+per2+"%");
}
}
if (o==2)
{
if (a>b)
{
double per=(100*a)/b;
System.out.println("The percentage is "+per+"%");
}
if (b>a)
{
double per2=(100*b)/a;
System.out.println("The percentage is "+per2+"%");
}
}
}
void power(double a,double b)
{
double pow=Math.pow(a,b);
System.out.println(a+" raised to "+b+" is equal to "+pow);
}
void squareroot(double a,double b)throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Which no. you want me to take out the square root of ? [give the number in the list below which correspons to that operation]");
System.out.println("1=> 1st given number");
System.out.println("2=> 2nd given number");
int h=Integer.parseInt(in.readLine());
if (h==1)
{
double sq=Math.sqrt(a);
System.out.println("Square root of "+a+" is "+sq);
}
if (h==2)
{
double sq2=Math.sqrt(b);
System.out.println("Square root of "+b+" is "+sq2);
}
}
void cuberoot(double a,double b)throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Which no. you want me to take out the cube root of ? [give the number in the list below which correspons to that operation]");
System.out.println("1=> 1st given number");
System.out.println("2=> 2nd given number");
int po=Integer.parseInt(in.readLine());
if (po==1)
{
double cr=Math.cbrt(a);
System.out.println("Cube root of "+a+" is "+cr);
}
if (po==2)
{
double cr2=Math.cbrt(b);
System.out.println("Cube root of "+b+" is "+cr2);
}
}
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Hi,I am a calculator");
System.out.println("As your real life calculator,I can do the same things");
System.out.println("But I can only operate on 2 numbers, one at a time");
System.out.println("And for every other operation,you have to exit and come back to the terminal window");
System.out.println("But there are some other useful operations available here as well");
System.out.println("So now,give me any 2 numbers on which you want me to operate");
double n1=Double.parseDouble(in.readLine());
double n2=Double.parseDouble(in.readLine());
calculator ob=new calculator();
System.out.println("What you want to do with these 2 numbers [give the number in the list below which correspons to that operation]");
System.out.println("1=> Addition");
System.out.println("2=> Subtraction");
System.out.println("3=> Multiplication");
System.out.println("4=> Division");
System.out.println("5=> Find the remainder of their division");
System.out.println("6=> Find the percentage of one to the other");
System.out.println("7=> Find the power of the 1st number to the 2nd number");
System.out.println("8=> Find the square root of anyone of the numbers");
System.out.println("9=> Find the cube root of anyone of the numbers");
int p=Integer.parseInt(in.readLine());
if (p==1)
{
ob.add(n1,n2);
}
if (p==2)
{
ob.subtract(n1,n2);
}
if (p==3)
{
ob.multiply(n1,n2);
}
if (p==4)
{
ob.divide(n1,n2);
}
if (p==5)
{
ob.findremainder(n1,n2);
}
if (p==6)
{
ob.percent(n1,n2);
}
if (p==7)
{
ob.power(n1,n2);
}
if (p==8)
{
ob.squareroot(n1,n2);
}
if (p==9)
{
ob.cuberoot(n1,n2);
}
System.out.println("So I hope you got your answer and you are satisfied with it");
System.out.println("Also, please give a feedback to my master");
System.out.println("Your feedback could help me to improve more and more");
System.out.println("And if I got time... I could conqueror the world");
System.out.println("Anyways...");
System.out.println("Thank you for accessing me and I hope you have a great day");
}
}
But here's the problem. At the end, when the user gets the result, I want the program to ask the user if he/she wants to give 2 more numbers to operate on or leave the calculator. If user wants to perform more operations, the code will get repeated, or if the user wants to leave the program, the program will end. But I don't know how to do the repeating part. Is anyone has a solution for this??
To repeat the question, you can use do {...} while(...);. I also changed the if(p==...) blocks to use switch(...) { case(...): ... }, as it is a perfect use-case for it.
You can change your main(String args[]) to:
public static void main(String args[])throws IOException {
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Hi,I am a calculator");
System.out.println("As your real life calculator,I can do the same things");
System.out.println("But I can only operate on 2 numbers, one at a time");
System.out.println("And for every other operation,you have to exit and come back to the terminal window");
System.out.println("But there are some other useful operations available here as well");
boolean shouldContinue;
do {
System.out.println("So now,give me any 2 numbers on which you want me to operate");
double n1=Double.parseDouble(in.readLine());
double n2=Double.parseDouble(in.readLine());
calculator ob=new calculator();
System.out.println("What you want to do with these 2 numbers [give the number in the list below which correspons to that operation]");
System.out.println("1=> Addition");
System.out.println("2=> Subtraction");
System.out.println("3=> Multiplication");
System.out.println("4=> Division");
System.out.println("5=> Find the remainder of their division");
System.out.println("6=> Find the percentage of one to the other");
System.out.println("7=> Find the power of the 1st number to the 2nd number");
System.out.println("8=> Find the square root of anyone of the numbers");
System.out.println("9=> Find the cube root of anyone of the numbers");
int p=Integer.parseInt(in.readLine());
switch(p) {
case 1:
ob.add(n1,n2);
break;
case 2:
ob.subtract(n1,n2);
break;
case 3:
ob.multiply(n1,n2);
break;
case 4:
ob.divide(n1,n2);
break;
case 5:
ob.findremainder(n1,n2);
break;
case 6:
ob.percent(n1,n2);
break;
case 7:
ob.power(n1,n2);
break;
case 8:
ob.squareroot(n1,n2);
break;
case 9:
ob.cuberoot(n1,n2);
break;
default:
System.out.println("Provided op-code not supported!");
}
System.out.println("Do you want to continue? (yes/no)");
String choice = in.readLine();
shouldContinue=choice.equalsIgnoreCase("yes");
} while(shouldContinue);
System.out.println("So I hope you got your answer and you are satisfied with it");
System.out.println("Also, please give a feedback to my master");
System.out.println("Your feedback could help me to improve more and more");
System.out.println("And if I got time... I could conqueror the world");
System.out.println("Anyways...");
System.out.println("Thank you for accessing me and I hope you have a great day");
}
You should use a do-while loop including a switch case.
For example,
int ch;
cout<<"\n********************MENU**************************";
cout<<"\n1.Addition";
cout<<"\n2.Subtraction";
cout<<"\n3.Exit";
do
{
cout<<"\nEnter the choice: ";
cin>>ch;
switch(ch)
{
case 1: //call adition function here
break;
case 2: //call substraction function here
break;
}
}while(ch!=3);
This will keep running until the user enters choice as 3 i.e Exit in this case.
When i run my code, I get an error in mimir (idk if you guys know about it but is a platform that teachers use to check the test cases of the code that you submit) saying that I a have a whitespace that shouldn't be there (check image).
1.) how can i fix that regarding my code.
and another issue which it drove me crazy is,
how can I make it so that when I enter a letter, In this case it was inputted 'f' (check the image) to display the "enter two integers:" just like the imagine. I tried different types of loops and changing the layout but i never got it to be correct.
p.d. Yes, this was a homework and was due yesterday. Even though i got a good grade on it, it still bugged me that I couldn't figure out these two things out.
import java.util.InputMismatchException;
import java.util.Scanner;
public class MathTeacher {
public static int addNumbers(int n1, int n2){
int add = n1+n2;
return add;
}
public static int subtractNumbers(int n1, int n2){
int subs = n1-n2;
return subs;
}
public static int multiplyNumbers(int n1, int n2){
int mulp = n1*n2;
return mulp;
}
public static int divideNumbers(int n1, int n2){
int div = n1/n2;
return div;
}
private static int getIntFromUser(Scanner scan) {
int x;
while (true) {
try {
x = scan.nextInt();
break;
} catch (InputMismatchException e) {
scan.next();
}
}
return x;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Welcome to *Mental Math Practice* where you can test your addition, subtraction, multiplication, and division.");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two integers: ");
String typeQuit= "";
do {
int choices;
int n1 = getIntFromUser(scanner);
int n2 = getIntFromUser(scanner);
System.out.println("Enter 1 to add the two numbers.");
System.out.println("Enter 2 to subtract the second number from the first number.");
System.out.println("Enter 3 to multiply the two numbers.");
System.out.println("Enter 4 to divide the first number by the second number.");
choices = scanner.nextInt();
switch (choices) {
case 1: {
int add = addNumbers(n1, n2);
System.out.println(add);
break;
}
case 2: {
int sub = subtractNumbers(n1, n2);
System.out.println(sub);
break;
}
case 3: {
int mulp = multiplyNumbers(n1, n2);
System.out.println(mulp);
break;
}
case 4: {
int div = divideNumbers(n1, n2);
System.out.println(div);
break;
}
}
System.out.println("Enter 'Quit' to end the program.");
typeQuit = scanner.next();
} while(!typeQuit.equals("Quit"));
}
}
So, I have a variable in a parent class that I am trying to change in a subclass with getter/setter methods. But, the value is just staying the same and I have no idea why.. What am I doing wrong? Any help is appreciated!
Here is a breakdown of the program: In the driver class, you choose what you want to do, then it uses the current value variable and a number you choose (operand2) to get the answer. The add, subtract, multiply and divide are in the memory calculator class. It can also clear, which sets the current value variable to zero. Now, we are adding a sub class to it that does exponents and logarithms.
specifics: The variable currentValue in the MemoryCalc class stays the same when I try to use the power or log methods in the ScientificMemCalc class. In that class it uses a getter method to get the current value and then attempts to use a setter method to change the current value. But nothing changes. And another problem: the getter method gets a zero value from the currentValue field.
Here is driver class with main method:
package ScientificMemCalc;
import java.util.Scanner;
import ScientificMemCalc.MemoryCalc;
public class ScientificCalcDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MemoryCalc calculator = new MemoryCalc();
ScientificMemCalc scientificCalc = new ScientificMemCalc();
int menu = 0;
double operand2, answer;
while (menu !=8) {
answer = calculator.getCurrentValue();
System.out.println("The current value is: " + answer);
menu = getMenuOption();
switch(menu) {
case 1:
// Add
operand2 = calculator.getOperand("What is the second number?: ");
calculator.add(operand2);
break;
case 2:
// Subtract
operand2 = calculator.getOperand("What is the second number?: ");
calculator.subtract(operand2);
break;
case 3:
// Multiply
operand2 = calculator.getOperand("What is the second number?: ");
calculator.multiply(operand2);
break;
case 4:
// Divide
operand2 = calculator.getOperand("What is the second number?: ");
calculator.divide(operand2);
break;
case 5:
// Power
operand2 = calculator.getOperand("What is the second number?: ");
scientificCalc.power(operand2);
break;
case 6:
// Logarithm
scientificCalc.log();
break;
case 7:
// Clear
operand2 = 0;
calculator.clear();
break;
case 8:
// Quit
System.out.println("Goodbye!");
break;
}
}
}
public static int getMenuOption() {
Scanner input = new Scanner(System.in);
int choice = 0;
// Display menu
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");
// Get menu input
System.out.print("What would you like to do?: ");
choice = input.nextInt();
while (choice < 1 || choice > 8) {
System.out.print("Invalid. Try again: ");
choice = input.nextInt();
}
return choice;
}
}
Here is the memory calculator class:
package ScientificMemCalc;
import java.util.Scanner;
public class MemoryCalc {
private double currentValue;
public double getOperand(String prompt) {
Scanner input = new Scanner(System.in);
System.out.print(prompt);
return input.nextDouble();
}
public double getCurrentValue() {
return currentValue;
}
public void setCurrentValue(double temp) {
currentValue = temp;
}
public void add(double operand2) {
// Add
currentValue += operand2;
}
public void subtract(double operand2) {
// Subtract
currentValue -= operand2;
}
public void multiply(double operand2) {
// Multiply
currentValue *= operand2;
}
public void divide(double operand2) {
// Divide
if (operand2 == 0) {
System.out.println("You cannot divide by zero!");
currentValue = Double.NaN;
}
else {
currentValue /= operand2;
}
}
public void clear() {
// Clear
currentValue = 0;
}
}
And finally the subclass to add scientific functions:
package ScientificMemCalc;
public class ScientificMemCalc extends MemoryCalc {
public void power(double operand2) {
// Power
double currentValue = getCurrentValue();
double temp = Math.pow(currentValue, operand2);
setCurrentValue(temp);
}
public void log() {
// Logarithm
double currentValue = getCurrentValue();
double temp = Math.log(currentValue);
setCurrentValue(temp);
}
}
Short answer
You're interacting with TWO DIFFERENT OBJECTS calculator and scientificCalc. They do not share any state.
What can you do?
Use scientificCalc for all the calculations. Thus the value will be one and only.
cons : the things can become much more complicated once you introduce another calculator types
Pass the calculator to constructor of ScientificMemCalc as a constuctor parameter.
cons : same as for #1
Use a separate class/object for storing the state. (as suggested by #user2864740)
Do not store the state inside the calculator (Why would you need that?). Pass all the operands (current state will be always operand #1) to the methods and return the result to caller:
int menu = 0;
double operand2, answer;
while (menu !=8) {
// You don't need this line
//answer = calculator.getCurrentValue();
System.out.println("The current value is: " + answer);
menu = getMenuOption();
switch(menu) {
case 1:
// Add
operand2 = calculator.getOperand("What is the second number?: ");
//
answer = calculator.add(answer, operand2);
break;
//... modify other operations in the same way
}
In the ScientificMemCalc class you extend MemoryCalc, this lets ScientificMemCalc call the methods created in MemoryCalc and provides access to its own set of variables in MemoryCalc. However, when creating two separate objects of each class, they will both have access to a variable of the same name and type, but they will be two separate variables in two separate objects.
One solution is passing in a MemoryCalc object into the constructor of the ScientificMemCalc class then calling the getCurrentValue()/setCurrentValue() methods in respects to that object. Allowing access to the same variable.
package scientificMemCalc;
public class ScientificMemCalc {
//store the MemoryCalc object so we have access to the needed data
private MemoryCalc memCalc;
//store the MemoryCalc
public ScientificMemCalc(MemoryCalc memCalc) {
this.memCalc = memCalc;
}
public void power(double operand2) {
// Power
double currentValue = memCalc.getCurrentValue();
double temp = Math.pow(currentValue, operand2);
memCalc.setCurrentValue(temp);
}
public void log() {
// Logarithm
double currentValue = memCalc.getCurrentValue();
double temp = Math.log(currentValue);
memCalc.setCurrentValue(temp);
}
}
Then when instantiating your ScientificMemCalc object pass in the reference for your MemoryCalc.
MemoryCalc calculator = new MemoryCalc();
ScientificMemCalc scientificCalc = new ScientificMemCalc(calculator);
Hope this helps!
In my class we needed to make a memory calculator in Java. Im really new to Java and had help making the program. Turned it in and the teacher said "Please separate the MemoryCalculator class from the class with the main() method. Currently the way you have created the class, there is no reason to create an instance of the class. But the point of the assignment is to use separate classes and objects." Its been a super long week and midterms and just lost at this time. Any help would be great.
import java.util.Scanner;
public class MemoryCalculator {
private double currentValue;
//Methods
//Scanner
public static int displayMenu(){
Scanner input = new Scanner(System.in);
System.out.print("Lets do some math! \nMenu \n1. Add \n2. Subtract \n3. Multiply \n4. Divide \n"
+ "5. Clear \n6. Quit \n\nWhat would you like to do? ");
int menuChoice = input.nextInt();
return menuChoice;
}
public static double getOperand(String prompt) {
Scanner input = new Scanner(System. in );
double operand;
System.out.println(prompt);
operand = input.nextDouble();
return operand;
}
//Current Value
//Gets
public double getCurrentValue() {
return currentValue;
}
//Setter
public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;
}
//Add
public void add(double operand2) {
currentValue += operand2;
}
//Subtract
public void subtract(double operand2) {
currentValue -= operand2;
}
//Multiply
public void multiply(double operand2) {
currentValue *= operand2;
}
//Divide
public void divide(double operand2) {
if (operand2==0){
setCurrentValue(0);
}
currentValue /=operand2;
}
//Clear
public void clear() {
currentValue = 0;
}
//Main part of the calculator
public static void main(String[] args) {
MemoryCalculator instance = new MemoryCalculator();
double operand;
boolean repeat = true;
while (repeat) {
System.out.println("The current value is: " + instance.getCurrentValue() + "\n");
int menuChoice;
menuChoice = displayMenu();
if (menuChoice > 6 || menuChoice < 1){
System.out.println("I'm sorry, " + menuChoice + " wasn't one of the options\n");
}
switch(menuChoice){
case 1:
operand = getOperand("What is the second number?");
instance.add(operand);
break;
case 2:
operand = getOperand("What is the second number?");
instance.subtract(operand);
break;
case 3:
operand = getOperand("What is the second number?");
instance.multiply(operand);
break;
case 4:
operand = getOperand("What is the second number?");
instance.divide(operand);
break;
case 5:
instance.clear();
break;
case 6:
System.out.println("Goodbye have a great day");
System.exit(0);
break;
}
}
}
}
What it looks like you did with your program was create one, single, class that holds all of the code for your calculator program, within which you instantiated an object of the same class.
What your teacher wants instead, is for you to have two separate classes, one which contains the code that makes the calculator work, and another class where you instantiate an object of the first class, and call the methods contained within that class.
For your assignment, what I would suggest would be to create a new class, perhaps called Main, where your program's Main() method will be, and keep all of the code for the calculator program in the MemoryCalculator class. From there, you can instantiate an object of MemoryCalculator class (which you already did, called instance) and use method calls to reference methods and attributes from within the MemoryCalculator class.
This may require reworking some of your code so that it runs properly, given that you'll be calling most of it from an object of the MemoryCalculator class, but it should be doable.
Hi I was attempting to create a calculator that can add subtract multiply and divide to challenge myself but find myself getting stuck around the switch part:(I will point out the errors within the switch message that say "The method addition etc(String[]) in the type addition etc is not applicable for the arguments ()." I believe the problem lies within the public void of the other classes.
Script:
public class ComputronCalc {
public static void main(String args[]) {
int mode;
mode = 1;
Addition ADD = new Addition();
Subtraction SUB = new Subtraction();
Multiplication MUL = new Multiplication();
Division DIV = new Division();
System.out.println("Hello welcome to the Computron fully functional calculator, coded by Samuel Cole, designed by Dwight Schrute.");
switch(mode) {
case 1:
ADD.Addition();<-----------addition is underlined in red
break;
case 2:
SUB.Subtraction();<-------------same
break;
case 3:
MUL.Multiplication();<---------------same
break;
case 4:
DIV.Division();<----------------same
break;
default:
System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
}
System.out.println("Thank you for choosing Computron.");
}
}
import java.util.Scanner;
public class Addition {
public void Addition(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum + snum;
System.out.println(answer);
}
}
import java.util.Scanner;
public class Multiplication {
public void Multiplication(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum * snum;
System.out.println(answer);
}
}
import java.util.Scanner;
public class Division {
public void Division(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum / snum;
System.out.println(answer);
}
}
note: I'm using eclipse so each class is on like a different page.
Your methods expect the argument "args", which you don't use. Remove it. For example:
public void Addition(String Args[]) {
becomes:
public void Addition() {
(by the way, your code does not follow Oracle Java Naming Convention)
While the acute problem is that you need to change your method signatures to not have any parameters or change the method invocation to send a parameter, I think there is a better solution you should concider.
Change the methods in your operation classes to be constructors:
public class Addition {
public Addition() {
//...
}
}
Then you do not need to instantiate all the operations for each run and the switch becomes:
switch(mode) {
case 1:
Addition();
break;
case 2:
Subtraction();
break;
case 3:
Multiplication();
break;
case 4:
Division();
break;
default:
System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
}
Do not have any parameter (in this program, the String[] args parameter) for the function inside the four operator classes. Your program should work without them.
public class Addition {
public Addition() {
//...
}
}
Same applies for other classes too.
public class Subtraction {
public Subtraction() {
//...
}
}
public class Multiplication{
public Multiplication() {
//...
}
}
public class Division {
public Division() {
//...
}
}