The document describes the development of a Java console-based calculator application. It includes 5 classes - an interface, a base Calculate class containing calculation methods, and Calculator/ScientificCalculator subclasses for basic and scientific operations. The main UseCalculator class allows the user to choose operation type and perform calculations via do-while loops until quitting.
WAP to create equivalent of a four function calculator. The program requires the user to enter two numbers and an operator. It then carries out the specified arithmetic operation: addition, subtraction, multiplication and division of two numbers.(using nested if statement)
WAP to create equivalent of a four function calculator. The program requires the user to enter two numbers and an operator. It then carries out the specified arithmetic operation: addition, subtraction, multiplication and division of two numbers.(using nested if statement)
Headache from using mathematical softwarePVS-Studio
It so happened that during some period of time I was discussing on the Internet, one would think, different topics: free alternatives of Matlab for universities and students, and finding errors in algorithms with the help of static code analysis. All these discussions were brought together by the terrible quality of the code of modern programs. In particular, it is about quality of software for mathematicians and scientists. Immediately there arises the question of the credibility to the calculations and studies conducted with the help of such programs. We will try to reflect on this topic and look for the errors.
Modeling an ODE: 3 different approaches - Part 2Scilab
In this tutorial we show how to model a physical system described by ODE using Xcos environment. The same model solution is also described in Scilab and Xcos + Modelica in two other tutorials.
In this tutorial the reader can learn about data fitting, interpolation and approximation in Scilab. Interpolation is very important in industrial applications for data visualization and metamodeling.
Operators In Java Part - 8
Like My Page:
www.facebook.com/pakcoders
What is Java? Part - 1
https://www.slideshare.net/MuhammadAtif231/what-is-java-part-1
History Of Java Part - 2
https://www.slideshare.net/MuhammadAtif231/history-of-java-part-2
Features Of Java Part - 3
https://www.slideshare.net/MuhammadAtif231/history-of-java-part-2
C++ Vs Java Part - 4
https://www.slideshare.net/MuhammadAtif231/c-vs-java-part-4
C programming, RTS Tech. Indore
This is a presentation to take your skills to next level. Hope you will like our work to make programming easier for you.
Feel free to contact for the online/offline batches.
Headache from using mathematical softwarePVS-Studio
It so happened that during some period of time I was discussing on the Internet, one would think, different topics: free alternatives of Matlab for universities and students, and finding errors in algorithms with the help of static code analysis. All these discussions were brought together by the terrible quality of the code of modern programs. In particular, it is about quality of software for mathematicians and scientists. Immediately there arises the question of the credibility to the calculations and studies conducted with the help of such programs. We will try to reflect on this topic and look for the errors.
Modeling an ODE: 3 different approaches - Part 2Scilab
In this tutorial we show how to model a physical system described by ODE using Xcos environment. The same model solution is also described in Scilab and Xcos + Modelica in two other tutorials.
In this tutorial the reader can learn about data fitting, interpolation and approximation in Scilab. Interpolation is very important in industrial applications for data visualization and metamodeling.
Operators In Java Part - 8
Like My Page:
www.facebook.com/pakcoders
What is Java? Part - 1
https://www.slideshare.net/MuhammadAtif231/what-is-java-part-1
History Of Java Part - 2
https://www.slideshare.net/MuhammadAtif231/history-of-java-part-2
Features Of Java Part - 3
https://www.slideshare.net/MuhammadAtif231/history-of-java-part-2
C++ Vs Java Part - 4
https://www.slideshare.net/MuhammadAtif231/c-vs-java-part-4
C programming, RTS Tech. Indore
This is a presentation to take your skills to next level. Hope you will like our work to make programming easier for you.
Feel free to contact for the online/offline batches.
Bottom of FormCreate your own FunctionFunctionsFor eac.docxAASTHA76
Bottom of Form
Create your own Function
Functions
For each discussion, provide a snipplet of pseudo-code for the Main using an example call to the Function as well as the pseudo-code for the Function.
For each discussion, do the problem you are assigned to as described by the letters below.
Discussion 1 - Using Pseudocode, create a Function that accepts one or more input Integer numbers and returns a float number. You should name your function appropriately as to what it does. Be sure to document your Function with header and in-line comments.
Provide a snipplet of psuedo-code for the Main using an example call to the Function.
Make sure the variable names in the Main are different that in the Function. In the Main, provide the prompts and get the user responses. Then pass the data into the Function. After the call to the Function include a print statements that indicates the returning value from the call to the Function.
Put Discussion 1 - problem no.X in the Subject area.
You are assigned the problem no. below as follows: If your Last Name begins with:
A-B - do no. 1
C-F - do no. 2
G-H - do no. 3
I-K - do no. 4
L - do no. 5
M-P - do no. 6
Q-T - do no. 7
U-Z - do no. 8
1)Calculate the Area of a circle. Input: one number
2)Calculate the Circumference a circle. Input: one number
3)Convert the temperature from Celcius to Farenheit Input: one number
4)Convert the temperature from Farenheit to Celcius. Input: one number
5)Calculate 6 times a number squared. Input: one number
6)Calculate 3 times the (sum of three numbers). Input: three numbers
7)Calculate the average of four numbers. Input: four numbers
8)Calculate 5 times the (difference of two numbers). Input two numbers
You may do additional problems, if you want.
Discussion 2:
Convert Discussion 1 to C-code. Don't for get to prototype your function before the main and to define your function after the main. Put Discussion 2 - problem no.X in the Subject area and submit a .txt (or .c) file for your code.
CMIS 102 Hands-On Lab
Week 6
Overview:
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, Analysis, Design(program design, pseudocode), Test Plan, and implementation with C code. The example provided uses sequential, repetition, selection statements and two user-defined function.
Program Description:
This program will provide options for a user to calculate the square or cube of a positive Integer input by a user. The program will prompt the user to enter an Integer and then prompt the user if they want to calculate the square of the cube of the number. Based on the inputs of the user, the program will output the square of the cube of the positive integer. The program will then print the Integer and square or cube of the integer based on the user’s original choice. The program will continue to prompt the user for
Integers and their calculation choice until the user enters a negative int ...
College for Women Department of Information Science WilheminaRossi174
College for Women
Department of Information Science
Kuwait University
College of Computing Sciences and Engineering
Department of Information Science
COMP1231
Web Programming
Assignment #1
Due Date: Friday, February 26th, 11:59 pm
Weight: 7% of Final Grade
COMP1231 - ASSIGNMENT 1 2
Table of Contents
COMP1231 Assignment 1 ............................................................................................................................ 3
Function 1: Student Information ................................................................................................................... 4
Function 2: Calculate Change ....................................................................................................................... 5
Function 3: Miles to Kilometers ................................................................................................................... 5
Function 4: Find Min and Max ..................................................................................................................... 6
Function 5: Count Occurrences..................................................................................................................... 6
Function 6: Convert Number Grade to Letter Grade .................................................................................... 7
Function 7: Reverse the Order of the Array .................................................................................................. 7
Function 8: School Days Remaining ............................................................................................................ 7
Function 9: Count Vowels ............................................................................................................................ 8
Function 10: Alphabetical Order................................................................................................................... 8
Submission Procedure and Rules ................................................................................................................ 10
Submission Procedure ............................................................................................................................. 10
COMP1231 - ASSIGNMENT 1 3
COMP1231 Assignment 1
Assignment Type: Individual Assignment
1. Description
In this assignment, you are to develop a JavaScript file that contains the implementation of 10 functions. Each
function is represented as one step, and marks will be awarded on the completion of each step (each function).
Each function is independent and solves a unique problem, as such, treat and implement each function in isolation
of the others, that’s is, you should only focus on one problem at a time.
PLEASE NOTE: Input validation is NOT required for any function parameter – You may assume that all
input p ...
CMIS 102 Hands-On Lab
Week 6
Overview:
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, Analysis, Design(program design, pseudocode), Test Plan, and implementation with C code. The example provided uses sequential, repetition, selection statements and two user-defined function.
Program Description:
This program will provide options for a user to calculate the square or cube of a positive Integer input by a user. The program will prompt the user to enter an Integer and then prompt the user if they want to calculate the square of the cube of the number. Based on the inputs of the user, the program will output the square of the cube of the positive integer. The program will then print the Integer and square or cube of the integer based on the user’s original choice. The program will continue to prompt the user for
Integers and their calculation choice until the user enters a negative integer. The square and cube calculations should be calculated using a function.
Analysis:
I will use sequential, selection, and repetition programming statements and functions for the cube and square calculations.
I will define three Integer variables: IntValue, MenuSelect, Results to store the Integer value input by the user, the Menu selection (1 for Square, 2 for Cube) of the user, and the results of the Square or Cube functions.
The Square function will take one Integer as input and return one Integer as the output. The calculation within the Square function is: Results = IntValue * IntValue
For example, if 10 was entered as the IntValue. Results = 10*10 = 100
The Cube function will take one Integer as input and return one Integer as the output. The calculation within the Cube function is: Results = IntValue * IntValue*IntValue
For example, if 10 was entered as the IntValue. Results = 10*10*10 = 1000
A repetition loop can be used to loop through iterations until a negative is entered: while(intValue > 0) (
…
End For
1
Program Design:
Main
· This program will provide options for a user to calculate the square
· or cube of a positive Integer input by a user.
· Declare variables
· Initialize loop variable intValue to positive value to start loop
· Loop While input is a positive number
//Prompt user for a number //Get user response
// Only perform menu and function calls if integer is positive If intValue > 0 Then
//Prompt user for selection Square or Cube
// "Enter 1 to calculate Square, 2 to Calculate Cube " If menuSelect == 1 Then
// Call the Square Function //Print results
Else If menuSelect == 2 Then // Call the Cube function //Print results
Else
//Print Invalid msg
End If //End of If menuSelect End If //End of If intValue > 0
//END While
End // End of Main program
· Square Function ------------------------------
//Calculates the square of an Integer
· Cube Function ------------------------------
//Calculates the cubeof an Integer
Tes ...
Procedure to create_the_calculator_application java
1. The Calculator Application
Learning Objectives
The development process of the Calculator application will aid the students to:
• Create a simple Java console application
• Understand the object-oriented concepts of inheritance, polymorphism and data hiding
• Create application which request input from users, validate, process the input received
and provide desired output.
• Use features of java like type conversion, interfaces, inheriting interfaces, looping and
branching, packages and I/O classes.
Understanding the Calculator Application
The Calculator application performs both basic and scientific operations. The application provides
user an option to choose between the basic mode and scientific mode. Based on the option
selected by the user, the application calls the corresponding class and the user can perform
various mathematical operations provided in the class. There is a base class in the application
which contains all the methods for calculation, basic as well as scientific. The application
validates the user input also and provides appropriate messages when wrong input is given by
the user.
Creating the Calculator Application
To create the Calculator application, 5 java files were created. First, an interface iCalc, with the
file name “iCalc.java” is created. Then, we create the base class Calculate, with the file name
“Calculate.java” which contains all the methods for calculation. After the base class, two classes,
Calculator and ScientificCalculator, with the file names as “Calculator.java” and
“ScientificCalculator.java” are created. These classes call the methods defined in the base class
Calculate. Class Calculator contains an instance of Class Calculate, whereas Class
ScientificCalculator inherits Class Calculate and then uses its methods. After creation of all the
above classes, a main class UseCalculate is created, with the file name “UseCalculate.java”
which provides creates instances of Class Calculator or Class ScientificCalculator, based on the
option selected by user.
Creating the Java Files
The iCalc Interface (iCalc.java)
Interface iCalc provides the structure of methods which can be used in any calculator application.
It contains the following two methods:
• doCalculation(): Declares a method for providing methods for calculation.
• getResult(): Declares a method for extracting the result of the calculation.
The Calculate Class (Calculate.java)
2. Class Calculate contains the business logic of the Calculator application. It contains the methods
for calculation of various mathematical operations like addition, divide and tangent. Class
Calculate uses interfaces by implementing Interface iCalc. The class contains following methods:
Method Description
Calculate() Default constructor for the class without any arguments.
Calculate(Double dblNum, Constructor containing two arguments. This constructor
char cOperator) is used for scientific calculations.
Calculate(int iFirstNum, char Constructor containing three arguments. This
cOperator, int iSecondNum) constructor is used for basic calculations.
doCalculation() Calculates the result based on the numbers and
operator inputted by the user. Overriding the
doCalculation function.of iCalc interface.
getResult() Prints the result of calculation. Overriding the getResult
function.of iCalc interface.
checkSecondNum() In case of division of two numbers, it checks for value 0
in the second number entered.
checkInt() Checks if basic calculation is performed.
checkDouble() Checks if scientific calculation is performed.
The Calculator Class (Calculator.java)
Class Calculator calculates basic operations, namely, addition, subtraction, multiplication and
division of two numbers. The class provides option to user to enter first number to be calculated,
then the operation to be performed and then, the second number to be used for calculation. The
input is received using java class BufferedReader. Class calculator creates an object of Class
Calculate, by calling its constructor by passing three arguments, First Number, Operator and
Second Number. After the creation of object of Class Calculate, doCalculation() method is called
followed by getResult() method, which presents the result of calculation to the user.
Class Calculator also uses a do-while loop to provide an option to the user perform multiple
calculations, till the user does not indicate the end of processing by typing ‘n’.
The ScientificCalculator Class (ScientificCalculator.java)
Class Calculator performs calculation of scientific operations, namely, sine, cosine, tangent and
log of a number. The class provides option to user to enter the operation to be performed and the
number to be calculated, The input is received using java class BufferedReader. Class
ScientificCalculator inherits Class Calculate to use its methods. The class passes the user
entered values to Class Calculate by calling its constructor having two arguments, Operator and
the Number. The class calls doCalculation() method which is followed by getResult() method,
which shows the result of calculation to the user.
Class ScientificCalculator also uses a do-while loop to provide an option to the user perform
multiple calculations, till the user does not indicate the end of processing by typing ‘n’.
The UseCalculator Class (UseCalculator.java)
Class Calculator provides two options to the user, Basic or Scientific. Based on the option
entered by the user, the class creates an instance of Class Calculator for Basic operations or an
instance of Class ScientificCalculator for scientific operations.
3. Class UseCalculator also uses a do-while loop to provide an option to the user perform multiple
calculations, till the user does not indicate the end of processing by typing ‘n’.
Generating the Class Files
Command for generating the class files:
javac <classname.java>
The steps for generating the class files for Calculator application are:
• Place all java files in a directory named as “Calculator”.
• In the command prompt, go to the directory where the java files are stored for the
Calculator application.
• Compile the following java files in the sequence given below using the command for
compiling the java files:
o iCalc.java
o Calculate.java
o Calculator.java
o ScientificCalculator.java
o UseCalculator.java
Working with the Calculator application
The steps for working with the Calculator application are:
• In the command prompt, go to the parent directory of “Calculator” directory which
contains the class files for Calculator application.
• Enter the following command to run the Calculator application:
java Calculator.UseCalculator
• Enter ‘b’ (for Basic operations) or ‘s’ (for scientific operations) depending on the
operations to be performed.
• If ‘b’ is entered, the following input is to be entered by the user:
o First Number
o Operator
o Second Number
• The result is shown on the command prompt based on the above values.
• Enter ‘y’ to continue or ‘n’ to discontinue using the application.
• If ‘s’ is entered, the following input is to be entered by the user:
o Operator
o Number
• The result is shown on the command prompt based on the above values.
• Enter ‘y’ to continue or ‘n’ to discontinue using the application.
4. Code for the Calculator Application
iCalc.java
/*************
-- Interface iCalc represents the basic methods for the Calculate class
-- Creates Interface Structure
-- Can be used for creating any class which would do any sort of calculations
*************/
// Adds the Interface to the Package
package Calculator;
//Interface Definition
interface iCalc
{
public void doCalculation();
public void getResult();
}
Calculate.java
/*************
-- Class Calculate has all methods for calculation as required by any Calculator classes
-- Implements an interface iCalc
*************/
// Adds the Class to the Package
package Calculator;
// Class Definition
class Calculate implements iCalc
{
private char Operator;
private int iFNum, iSNum;
private Double dblNumber=new Double(0);
private Double dblResult=new Double(0);
private int iResult=0;
private boolean typeDouble=false;
private boolean typeInt=false;
// Defines a constructor for scientific calculations
public Calculate()
{}
public Calculate(Double dblNum, char cOperator)
{
dblNumber=dblNum;
Operator=cOperator;
typeDouble=true;
5. }
// Defines a constructor for basic calculations
public Calculate(int iFirstNum, char cOperator, int iSecondNum)
{
iFNum=iFirstNum;
iSNum=iSecondNum;
Operator=cOperator;
typeInt=true;
}
// Calculates the Result based on the operator selected by the user
public void doCalculation()
{
iResult=0;
dblResult=0.0;
switch (Operator)
{
case '+':
checkInt();
iResult = iFNum + iSNum;
break;
case '-':
checkInt();
iResult = iFNum - iSNum;
break;
case '*':
checkInt();
iResult = iFNum * iSNum;
break;
case '/':
checkInt();
if(!checkSecondNum())
{
iResult = iFNum / iSNum;
break;
}
case 'S':
case 's':
checkDouble();
dblResult = Math.sin(dblNumber);
break;
case 'C':
case 'c':
checkDouble();
dblResult = Math.cos(dblNumber);
break;
case 'T':
case 't':
6. checkDouble();
dblResult = Math.tan(dblNumber);
break;
case 'L':
case 'l':
checkDouble();
dblResult = Math.log(dblNumber);
break;
default :
iResult=0;
dblResult=0.0;
System.out.println("***Operation Not Available. Please select
any of the available options.***");
break;
}
}
// Displays the result of calculation to the user
public void getResult()
{
if(typeInt)
{
System.out.println("The result is: " + iResult);
}
else if(typeDouble)
{
System.out.println("The result is: " + dblResult);
}
}
// Checks for zero
public boolean checkSecondNum()
{
if(iSNum==0)
{
System.out.println("Zero Not allowed");
System.exit(0);
return true;
}
else
{
return false;
}
}
public void checkInt()
{
if(!typeInt)
{
iResult=0;
System.out.println("***Operation Not Available. Please select any of the
available options.***");
System.exit(0);
}
7. }
public void checkDouble()
{
if(!typeDouble)
{
dblResult=0.0;
System.out.println("***Operation Not Available. Please select any of the
available options.***");
System.exit(0);
}
}
}
Calculator.java
/*************
-- Class Calculator Performs basic operations like Add, Substract, Multiply, Division for two
numbers
-- Uses class Calculate by creating its objects and then calling its methods
*************/
// Adds the Class to the Package
package Calculator;
// Imports Required Packages
import java.io.*;
// Class Definition
class Calculator
{
public void Calc() throws java.io.IOException
{
boolean next;
do
{
Integer iFirstNumber=new Integer(0);
Integer iSecondNumber=new Integer(0);
BufferedReader buffer
= new BufferedReader(new InputStreamReader(System.in));
// Gets User Input
System.out.println("Please enter First Number: ");
System.out.flush();
try
{
iFirstNumber=Integer.parseInt(buffer.readLine());
}
catch(NumberFormatException e)
8. {
System.out.println("***Please provide numeric values.***");
System.exit(0);
}
System.out.println("Please enter the Operation (Add : +, Minus : -,
Product : *, Divide : /):");
System.out.flush();
String option=buffer.readLine();
System.out.println("Please enter Second Number: ");
System.out.flush();
try
{
iSecondNumber=Integer.parseInt(buffer.readLine(),10);
}
catch(NumberFormatException e)
{
System.out.println("***Please provide numeric values.***");
System.exit(0);
}
if(option.length()==1)
{
// Creates Calculate Class Instance
Calculate c= new
Calculate(iFirstNumber,option.charAt(0),iSecondNumber);
// Calls the class methods
c.doCalculation();
c.getResult();
}
else
{
System.out.println("***Operation Not Available. Please select
any of the available options.***");
}
// Checks if the user would like to compute again
System.out.println("Would you like to calculate again (y/n)?");
System.out.flush();
char response=(char)buffer.read();
if ((response=='y') || (response=='Y'))
{
next=false;
}
else
{
next=true;
}
}while (!next);
}
9. }
ScientificCalculator.java
/*************
-- Class ScientificCalculator Performs scientific calculations like Sine, Cosine, Tangent and Log of
a number
-- Inherits class Calculate
-- Methods of Super class Calculate can be directly called by using the object of this Sub class
ScientificCalculator
*************/
// Adds the Class to the Package
package Calculator;
// Imports Required Packages
import java.io.*;
// Class Definition
class ScientificCalculator extends Calculate
{
char Operator;
Double dblNumber=new Double(0);
ScientificCalculator(){}
ScientificCalculator(double dblNumber,char Operator)
{
// Calls Super Class Constructor
super(dblNumber,Operator);
this.Operator=Operator;
this.dblNumber=dblNumber;
}
public void Calc() throws java.io.IOException
{
boolean next;
do
{
Double d=new Double(0);
BufferedReader buffer
= new BufferedReader(new InputStreamReader(System.in));
// Gets User Input
System.out.println("Please enter the Operation (Sine-s, Cosine-c,
Tangent-t, Log-l):");
System.out.flush();
String option=buffer.readLine();
System.out.println("Please enter a Value: ");
System.out.flush();
10. try
{
d=Double.valueOf(buffer.readLine());
}
catch(NumberFormatException e)
{
System.out.println("***Please provide numeric values.***");
System.exit(0);
}
if(option.length()==1)
{
// Creates Class Instance
ScientificCalculator sc=new
ScientificCalculator(d,option.charAt(0));
// Calls Super Class Methods
sc.doCalculation();
sc.getResult();
}
else
{
System.out.println("***Operation Not Available. Please select
any of the available options.***");
}
// Checks if the user would like to compute again
System.out.println("Would you like to calculate again (y/n)?");
System.out.flush();
char aa=(char)buffer.read();
if ((aa=='y') || (aa=='Y'))
{
next=false;
}
else
{
next=true;
}
}while (!next);
}
}
UseCalculator.java
/*************
-- Class UseCalculator is the Main Class for the Calculator Application
-- Provides options to the user: Basic or Scientific Calculator
*************/
// Adds the Class to the Package
package Calculator;
11. // Imports Required Packages
import java.io.*;
// Class Definition
class UseCalculator
{
public static void main(String[] args) throws java.io.IOException
{
BufferedReader buffer
= new BufferedReader(new InputStreamReader(System.in));
// Gets User Input
System.out.println("Select the Calculator: Basic - B or Scientific - S.");
System.out.flush();
String option=buffer.readLine();
if(option.length()==1)
{
if (option.equals("B") || option.equals("b"))
{
// Calls the Basic Calculator Application
Calculator c=new Calculator();
c.Calc();
}
else if(option.equals("S") || option.equals("s"))
{
// Calls the Scientific Calculator Application
ScientificCalculator sc=new ScientificCalculator();
sc.Calc();
}
else
{
System.out.println("***Please enter option 'B' or 'S'.***");
}
}
else
{
System.out.println("***Please enter option 'B' or 'S'.***");
}
}
}
Exercises:
1. Write the java code to calculate scientific operations like asin, atan and acos in the
Calculator application.
2. Write the java code to use both, basic and scientific operations available in Class
Calculate using multiple inheritance.