Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input and create an object of the ScientificCalculator Class with those two numbers. 2. Then call the 8 operations one by one sequentially and display the result. Code must be written in Java.
(Sample input/output: Enter number 1: 2 Enter number 2: 3 Add() result is : 5 Sub() result is: -1 ……. Power() result is: 8 …… sqrtNum1 result is:)
The Problem:
Given the following class diagram (see Chapter 3 Slide 34 and 36
if you are not familiar
with the diagram).
The diagram is
self
-explanatory
. Remember
“–“ means private and
“+” means public
in the diagram
.
Conver
t the class diagram into a java code. You can easily see the class name, variables and methods from
the diagram.
The diagram says that
Scientific
Calculator
class
inherits
the
Calculator
class. In many operations in the
scientific calculator class
, you can utilize the
java.Math class
’s methods
(https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
).
Calculator
-
num1: double
-
num2: double
<<constructor>>Calculator(
num1: double, num2:double)
+set
Num1(num1: double)
+setNum2(num2: double)
+getNum1(): double
+getNum2(): double
+add(): double
+sub(): double
//num1
-num2
+mul(): double
+div(): double
//num1/num2
Create a CalculatorTest class that contains the main method.
1.
Get two double numbers as input and create an object of the Scientific
Calculator Class
with
those two numbers.
2.
The
n call
the
8 operations one by one sequentially
and display the result.
ScientificCalculator
<<constructor>>Calculator(num1:
double, num2:double)
//don’t forget to call
the super class’s constructor from here
. Otherwise it willshow error as we don’t have a
constructor without any parameter.
+power(): double //returns num1 ^ num2
+reminder(): int
//returns the reminder of num1/num2 as integer. So,
convert num1 and num2 to int before performing the operation
+sinNum1(): double
//r
eturns the trigonometric sine of num1.
+sqrtNum1(): double
//returns the square root o
f Num1
Sample input/output:
Enter number 1: 2
Enter number 2: 3
Add() result is : 5
Sub() result is: -
1
.......
Power()
result is: 8
......
sqrtNum1 result is:
The code structure should look like the following
. Note
that
all the classes ar
e written in a single file.
So, in eclipse you can just create one class and write everything in the same file
:
File name: CalculatorTest.java
//all the require import statements
class Caclulator {
class body
}
Class .....{
Class body
}
Public class CalculatorTest{
Class body.
}
Program:
import java.util.Scanner;
import java.text.DecimalFormat;
class Calculator
{
private double num1;
private double num2;
Calculator(double num1, double num2)
{
this.num1 = num1;
this.num2 = num2;
}
void setNum1(double num1)
{
this.num1 = num1;
}
double getNum1()
{
return num1;
}
void setNum2(double num2)
{
this.num2 = num2;
}
double getNum2()
{
return num2;
}
double add()
{
return num1+num2;
}
double sub()
{
return num1-num2;
}
double mul()
{
return num1*num2;
}
double div()
{
return num1/num2;
}
}
class ScientificCalculator extends Calculator
{
ScientificCalculator(double num1, double num2)
{
super(num1,num2);
}
double power()
{
double num1 = super.getNum1();
double num2 = super.getNum2();
return Math.pow(num1,num2);
}
int remainder()
{
double num1 = super.getNum1();
double num2 = super.getNum2();
Double d1 = new Double(num1);
int first = d1.intValue();
Double d2 = new Double(num2);
int second = d2.intValue();
return first % second;
}
double sinNum1()
{
double num1 = super.getNum1();
return Math.sin(num1);
}
double sqrtNum1()
{
double num1 = super.getNum1();
return Math.sqrt(num1);
}
}
public class CalculatorTest
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number1: ");
double firstNum = s.nextDouble();
System.out.print("Enter number2: ");
double secondNum = s.nextDouble();
ScientificCalculator cal = new
ScientificCalculator(firstNum,secondNum);
//To print values without decimal values
DecimalFormat form = new DecimalFormat("#");
//To print values with two decimal places
DecimalFormat form2 = new DecimalFormat("#.##");
System.out.println("Add() result is : "+
form.format(cal.add()));
System.out.println("Sub() result is : "+
form.format(cal.sub()));
System.out.println("Mul() result is : "+
form.format(cal.mul()));
System.out.println("Div() result is : "+
form2.format(cal.div()));
System.out.println("Power() result is : "+
form.format(cal.power()));
System.out.println("Remainder() result is : "+
form.format(cal.remainder()));
System.out.println("SineNum1() result is : "+
form2.format(cal.sinNum1()));
System.out.println("SqrtNum1() result is : "+
form2.format(cal.sqrtNum1()));
}
}
Output:
Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input...
JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator { public static void main(String[] args) { // Generate random numbers int num1,num2; num1 =(int)(Math.random()*100+1);...
Write a class called TemperatureFile. • The class has one data attribute: __filename • Write getter and setter for the data attribute. • Write a “calculateAverage” function (signature below) to calculate and return average temperature of all temperatures in the file. def calculateAverage(self): • Handle possible exceptions Write a main function: • Create a file ('Temperatures.txt’) and then use “write” method to write temperature values on each line. • Create an object of the TemperaureFile with the filename (‘Temperatures.txt’) just...
Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two number and clicks on an operation button, the result should be displayed in the label. Can some please help my code that i came up with just displays a pop box with no words or nothing! I'm not the best at coding. Any tips or advice...
This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...
SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){ int num1,num2; cout << "Enter two numbers "<< endl; cout << "First :"; cin >> num1; cout << "Second :"; cin >>num2; int result=num1+num2; cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result; ...
LE 7.2 Purpose: To learn how to create and use stand-alone class programs (classes withouta main method). When you take code out of the application class and put it in a class of its own, you make it reusable Prep Work: Chapter 7 Figure 7.6 shows you how an application class uses the services of a Java class in Figure 7.5. Although the code for LE 7.2 is different than the code in these figures, the concept is the same....
i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...
java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...
c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...