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() {
//...
}
}
Related
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!
I'm trying to write JUnit tests for a program that converts Fahrenheit degrees to Celsius degrees (and opposite). So my class looks like:
package asdf;
import java.util.Scanner;
public class UnitTesting {
public UnitTesting()
{
}
public int returnInt()
{
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
return x;
}
public double returnDouble()
{
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
return x;
}
public double convertCtoF(double c) {
return 1.8*c+32;
}
public double convertFtoC(double f) {
return (5.0/9.0*(f-32));
}
public void menu()
{
int a;
do {
System.out.println("1 - convert C degrees to F degrees");
System.out.println("2 - convert F degrees to C degrees");
System.out.println("0 - exit");
a = returnInt();
switch(a)
{
case 1:
System.out.println(convertCtoF(returnDouble()));
break;
case 2:
System.out.println(convertFtoC(returnDouble()));
break;
case 0:
System.out.println("Bye!");
break;
default:
System.out.println("Choose 1, 2 or 0");
break;
}
}
while(a!=0);
}
public static void main(String [] args)
{
UnitTesting ut = new UnitTesting();
ut.menu();
}
}
test class:
package asdf;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class UnitTestingTests {
private UnitTesting ut;
#BeforeEach
public void init() {
ut = new UnitTesting();
}
#Test
void checkIfInputMismatch() {
ut.returnDouble();
//some_kind_of_assert
}
}
Since I wanted to test the user input's for various mismatch exceptions (NumberFormat, InputMismatch etc.) but I have no idea if this approach is possible for current methods returnDouble() and returnInt(). I was thinking of changing scanner to read input as a string (nextLine) but then extra parsing method would be needed. What would be needed to alter the methods/class to check if input is a number at all, or has a good format given by the user - in an "elegant" way?
Thank you
Some common techniques include:
Separate user interaction and program logic (here temperature conversion) into separate classes. Unit test your logic alone. Very often one does not unit test the user interaction, but integration tests the entire program including user interaction (you may use JUnit or some other tool for this).
Don’t create a new Scanner every time you read input. Use the same Scaner throughout. In this case it won’t be too hard to inject a different Scanner that reads from some other source than System.in for test purposes (if you really insisted on creating a new Scanner you might have method that in production creates a Scanner that reads from System.in and in test creates one that reads from some other source; but as I said, don’t bother).
You may also set System.in to some other input stream than the keyboard for test purposes. And/or similarly rewire System.out to a different output stream that allows to test what is written to it.
To control the input is integer, double or an invalid input, you need to take input as string. Then you can use defined parse methods of wrapper classes.
package asdf;
import java.util.Scanner;
public class UnitTesting {
public int returnInt()
{
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
try{
int x = Integer.parseInt(s);
return x;
}catch(NumberFormatException ex){ // handle your exception, there you can give some messages to user.
System.out.println("The input is invalid for integer.");
}
}
public double returnDouble()
{
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
try{
double x = Integer.parseDouble(s);
return x;
}catch(NumberFormatException ex){ // handle your exception, there you can give some messages to user.
System.out.println("The input is invalid for double.");
}
}
public double convertCtoF(double c) {
return 1.8*c+32;
}
public double convertFtoC(double f) {
return (5.0/9.0*(f-32));
}
public void menu()
{
int a;
do {
System.out.println("1 - convert C degrees to F degrees");
System.out.println("2 - convert F degrees to C degrees");
System.out.println("0 - exit");
a = returnInt();
switch(a)
{
case 1:
System.out.println(convertCtoF(returnDouble()));
break;
case 2:
System.out.println(convertFtoC(returnDouble()));
break;
case 0:
System.out.println("Bye!");
break;
default:
System.out.println("Choose 1, 2 or 0");
break;
}
}
while(a!=0);
}
public static void main(String [] args)
{
UnitTesting ut = new UnitTesting();
ut.menu();
}
}
And the other way to control : Pattern matching
String input=...;
String pattern ="-?\\d+";
if(input.matches("-?\\d+")){ // any positive or negetive integer or not!
...
}
I created a library System Rules that provide rules for populating System.in and reading output from System.out. Your Test may look like this:
package asdf;
import static org.junit.Assert.*;
import org.junit.Test;
public class UnitTestingTests {
#Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
private UnitTesting ut;
#Test
void checkIfInputMismatch() {
systemInMock.provideLines("1.23");
double value = ut.returnDouble();
//some_kind_of_assert
}
}
Unfortunately System Rules does only support JUnit 4. I'm working on a test framework independent alternative for System Rules.
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.
I am trying to make a simple calculator using multiple classes. I can make one in one class, but now I would like to try making one using two classes. I am having trouble with calling some variables (fnum, snum, answer) from the OperationClass.
If I run the program as it's written now I get 0 returned for the answer.
I tried making the variables fnum, snum and answer in OperationClass public but that did not work.
Also I tried public class OperationClass extends Calculator. That did not work either.
I know i'm missing something very simple. I just can't pin point it.
Calculator Class:
import java.util.Scanner;
public class Calculator{
public static void main(String[] args) {
OperationClass operationObject = new OperationClass();
Scanner input = new Scanner(System.in);
double userInput;
int userChoice;
System.out.print("Please Enter The First Number: ");
userInput = input.nextDouble();
System.out.print("Please Enter The Second Number: ");
userInput = input.nextDouble();
System.out.println("Please Select Operation to Perform:");
System.out.println("1-Addition");
System.out.println("2-Subtraction");
System.out.println("3-Division");
System.out.println("4-Multiplication");
userChoice = input.nextInt();
switch (userChoice){
case 1:
operationObject.addition();
System.out.println();
break;
case 2:
operationObject.subtraction();
break;
case 3:
operationObject.division();
break;
case 4:
operationObject.multiplication();
break;
}
}
}
OperationClass:
public class OperationClass{
double fnum, snum, answer;
public void addition(){
answer = fnum + snum;
System.out.println(answer);
}
public void subtraction(){
answer = fnum - snum;
System.out.println(answer);
}
public void division(){
answer = fnum / snum;
System.out.println(answer);
}
public void multiplication(){
answer = fnum * snum;
System.out.println(answer);
}
}
You aren't setting fnum and snum in OperationClass to anything, so their value is zero. Delete those member variables, and change your addition() subtraction() etc. methods to take them as parameters, like this:
public void addition(double fnum, double snum) {
answer = fnum + snum;
System.out.println(answer);
}
First, you need to store the two inputed values to different variables, so the second won't overwrite the first:
System.out.print("Please Enter The First Number: ");
fnum = input.nextDouble();
System.out.print("Please Enter The Second Number: ");
snum = input.nextDouble();
Once inputed, you can pass these variables to the OperationClass's methods:
public class OperationClass{
public void addition(double fnum, double snum){
double answer = fnum + snum;
System.out.println(answer);
}
public void subtraction(double fnum, double snum){
double answer = fnum - snum;
System.out.println(answer);
}
public void division(double fnum, double snum){
double answer = fnum / snum;
System.out.println(answer);
}
public void multiplication(double fnum, double snum){
double answer = fnum * snum;
System.out.println(answer);
}
}
You need to pass the values entered by the user to the OperationClass class.
There are multiple ways of doing this.
1) Option one. Change the methods to accept some parameters, for example:
public double addition(double fnum, double snum ){
return fnum+snum;
}
and to get the result in the Calculator you can do this:
case 1:
System.out.println(operationObject.addition(userInput1, userInput2));
break;
You have to store the user input in 2 different variables.
2) Store the user's input in the OperationClass as class variables.
public class OperationClass{
.....
public void setFNum(double fnum){
this.fnum = fnum
}
public void setSNum(double snum){
this.snum = snum
}
......
}
and to pass the values from the Calculator you can do this.
public static void main(String[] args) {
....
OperationClass operationObject = new OperationClass();
Scanner input = new Scanner(System.in);
double userInput;
userInput = input.nextDouble();
operationObject.setFNum(userInput);
userInput = input.nextDouble();
operationObject.setSNum(userInput);
....
}
You collect the two numbers into one variable called userInput. The first gets overwritten by the second, then you never reference it again. For your OperationClass to do something with those values, you should have methods to set the values, or pass them to the operation being performed as parameters.
For the first case you could add setter methods like this:
public void setFNum(double f) {
fnum = f;
}
public void setSNum(double s) {
snum = s;
}
Then in main, after getting a value from the user, set it using those methods.
System.out.print("Please Enter The First Number: ");
userInput = input.nextDouble();
operationObject.setFNum(userInput);
System.out.print("Please Enter The Second Number: ");
userInput = input.nextDouble();
operationObject.setSNum(userInput);
A second way is that you could make the methods in OperationClass take both numbers as parameters and never set it internally.
public void multiplication(double fnum, double snum){
answer = fnum * snum;
System.out.println(answer);
}
You need to pass the first and second ints as parameters. This involves re-writing your method objects to accept 2 parameters, or creating a constructor that accepts 2 parameters and instantiating the object after the numbers have been inputted.
To create the constructor, (which will allow you to set the variables in the class and use them):
public OperationClass(int fnum, int snum){
this.fnum = fnum;
this.snum = snum;
}
Then you call it when instantiating the object (again, after the numbers have been inputted) using:
OperationClass operationObject = new OperationClass(firstInput, secondInput);
You'll also need to modify your input so that the 2 inputs go in different variables. This is so that they can be passed as parameters.
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 :)