i am a beginner at java language and i use “text pad”. i have a problem with my simple program. my task is to input 2 values and show the “sum”,”difference”,”product” and “quotient” altogether. (simple right?) in which , here below is the class that supposed to be doing the job of arithmetic. in which is “correct” as i compiled.
public class mathclass
{
int x;
int y;
int total;
void add ()
{
total = x+y;
}
void sub ()
{
total = x-y;
}
void multi ()
{
total = x*y;
}
void div ()
{
total = x/y;
}
}
And here is the main program that supposed to be the input and output of the program.
my problem here is that i can’t pass the 2 variables (num1 and num2) to “mathclass”
i did research on how to pass 2 variables to a another class. but there is nothing same to mine that i have. i did use some like the putting “private or public” on the variables.
my teacher said to use the BufferedReader for input. and i am having a hard time how to get this program right. (sorry if i had wrong english(if i am wrong. ))
import java.io.*;
public class mathmain
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[]args)throws IOException
{
mathclass math1 = new mathclass();
System.out.print("Enter 1st Number :");
num1 = Integer.parseInt(br.readLine());
System.out.println(" ");
System.out.print("Enter 2nd Number :");
num2 = Integer.parseInt(br.readLine());
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
math1.add();
{
System.out.print("Sum : ");
System.out.println(math1.total);
}
System.out.println(" ");
math1.sub();
{
System.out.print("Difference : ");
System.out.println(math1.total);
}
System.out.println(" ");
math1.multi();
{
System.out.print("Product : ");
System.out.println(math1.total);
}
System.out.println(" ");
math1.div();
{
System.out.print("Quotient : ");
System.out.println(math1.total);
}
}
}
It’s really not clear what you’re trying to do here. (Why doesn’t
addtake two arguments for instance?)Perhaps your after something like this:
However, I would encourage you to use return values and use parameters:
and then use the class like