Java math operations



Posted : 1 week ago

COSC2436 – LAB1

 

Note: in the instruction of the lab change “yourLastName” to your last name

TITLE: Review data type class and Driver class – UML, pseudo-code – Math Operations

Time to complete: one week

COURSE OBJECTIVES – LEARNING OUTCOME

LO1

-Review premitive data types; apply math operations on numeric variables

-Review control structures, user defined functions

-Review data type class

-analysis, design, using UML, pseudo-code, flowchart

LAB OBJECTIVES

-Declare variables of int, double, String; Apply math operations on numeric variables

-Write the code of data type class: data type members, constructors, mutator methods, accessor methods, toString()

-apply if.. else, switch, for, do…while, while loop

-manage menu to re-display after finishing

-manipulate the output to format the output

 

SKILL REQUIRED TO DO THIS LAB

To do this lab, students have to know:

-Draw UML of data type class

-Write pseudo-code or draw flowchart of algorithm

-Review the syntax to create a data type class with data members,

constructors, mutator accessor methods, method toString

-Review how to declare an objejct in main(), how to access the methods of data type

 classes from main()

-Review control structure: if..else, switch, do..while loop

-How to set the decimal digits (2) of a decimal numbers

 DecimalFormat decimalFormat = new DecimalFormat("#.00");

HOW TO DO  EACH PART

From now and on yourLastName will be changed to your last name

 

*Step1: Read the requirement of each part; write the pseudo-code in a word document

by listing the step by step what you suppose to do in main() and then save it with the name as Lab1_pseudoCode_yourLastName

*Step2:

   -start editor eClipse, create the project à project name:      

                        FA2019_LAB1PART1_yourLastName (part 1)

                        FA2019_LAB1PART2_yourLastName (part 2)

   -add class:

                        DataTypeClass_yourLastName.java (part1)

                        DriverClass_yourLastName.java (part1)

 

                        MathOperation.java (part2)

                        MathCalculator_yourLastName.java (part 2)

 

   - In the file DriverClass_yourLastName.cpp (part1) or MathCalculator_yourLastName.cpp, type the follwing lines. This is the template of a java driver class. The java program starts with main()

               *Each statement should end by semi-colon “;”

               *The lines starting with // are the comment lines.

                 The compiler will not read the comment lines

 

public class nameOfClass

{

public static void main(String[ ] args)

{

         //add the code here

        

}

}

 

*Step3: follow step by step in the pseudo-code to write the java code

*Step4: compile and run the program

*Step5: debug if there is any errors to complete the program 

 

LAB1 PART 1

FOR THE DATA TYPE CLASS:

-Add the class to the project of part 1 with the name of class is DataTypeClass_yourLastName based on the following UML (You can use the table with 1 column x 3 rows in words

 

---------------------------------------------------------------------------------------

                                       DataTypeClass_Smith

----------------------------------------------------------------------------------------

   -intVariable: int

   -floatVariable: float

   -stringVariable: String

 -----------------------------------------------------------------------------------------

   + DataTypeClass_Smith()

   + DataTypeClass_Smith(intVar:int, floatVar:float, stringVar:String)

   + setIntVariable(intVar: int):void

   + getStringVariable(): String

   + toString(): String

-----------------------------------------------------------------------------------------

 

The following is a picture of UML:

 

 

public class DataTypeClass_Smith

{

      //add the code here

}

 

-Type the following lines into the file DataTypeClass_yourLastName then answer 6 questions listed on each of the following parts

 

//Question1: What is the name of the following lines in the data type class?

private int intVariable;

private float floatVariable;

private String stringVariable;

 

 

//Question2: What is the name of the following lines in the data type class?

public DataTypeClass_Smith()

{

         intVariable = 0;

         floatVariable = 0.0;

         stringVariable = “aString”;

}

 

//Question3: What is the name of the following lines in the data type class?

public DataTypeClass_Smith( int intVar, float floatVar, String stringVar)

{

      intVariable = intVar;

      floatVariable = floatVar;

      stringVariable = stringVar;

}

 

//Question4: What is the name of the following method in the data type class?

public void setIntVariable(int intVar)

{

      intVariable = intVar;

}

 

//Question5: What is the name of the following method in the data type class?

public float getFloatVariable(0

{

     return floatVariable;

}

 

//Question6: What is the purpose of the following method in the data type class?

public String toString()

{

      String str = “My name: James Smith\n” +

                           “The output is: \n” +

                          “Integer number: “ + intVariable + “\n” +

                          “Decimal number: “ + floatVariable + “\n” +

                          “String is: “ + stringVariable + “\n”;

      return  str;

}

 

FOR THE DRIVER CLASS

Add to the project of part 1 the class with the name as DriverClass_yourLastName

In the main() type the following lines of code:

 

import java.util.Scanner;

 

public class DriverClass_Smith

{

     int intValue;

     float floatValue;

     String stringValue;

    

     //Read input from the keyboard

    System.out.println(“Enter an integer number: “ );

    intValue = keyboard.nextInt();

 

    System.out.println(“Enter a decimal number: “ );

    floatValue = keyboard.nextFloat();

 

    System.out.println(“Enter a string: “);

    stringValue = keyboard.nextLine();

 

     //Declare an object of class DataTypeClass_Smith

    DataTypeClass_Smith object = new DataTypeClass_Smith(intValue, floatValue, stringValue);

 

    //print the output

    System.out.println(object);

}

 

 

 Requirement:

 -You have to change Smith to your last name

 -Change James Smith to your full name

 -Add the file name as the first comment line at the top of each class

 -Get the output of the part 1 then paste it after the answers of 6 above question

 -write the comment on each part in both classes

 

COMPILE AND RUN THE PART1 TO GET THE OUTPUT

 

LAB1 PART 2

Download the data type class named as MathOperation from the eCampus.

Create the project of part 2 then add class as a data type class and class MathCalculator_yourLastName as a driver class with main() of part 2

 

  1. Draw UML of class MathOperation (See the topic about UML at TIP FOR LAB on eCampus)
  2. Create the pseudo-code or draw flowchart of the main of a driver class based on the requirement listed below (see the topic about pseudo-code or flowchart at TIP FOR LAB on eCampus)
  3. Copy the content of the class MathOperation downloaded from eCampus to the data type class you have added to the part 2

 

      Requirement: Provide the application that first displays the following line and menu:

                 File name: MathCalculator_Smith

                 MENU – CALCULATOR ON TWO NUMBERS

1.       Add two integers

2.       Subtract two integers

3.       Multiply two integers

4.       Divide two integers

5.       Add two decimal numbers

6.       Subtract two decimal numbers

7.       Multiply two decimal numbers

8.       Divide two decimal numbers

0.       Exit

                Enter a number from 0 to 8 to continue:

 

      Based on the number that users enter to continue ask for input:

      -If users enter 1 to 4: ask for two integer numbers

     -if users  enter 5 to 8: ask for two decimal numbers

 

      Then pass two numbers read from input to the object of class MathOperation

      Use the object to display the result. The result in one of the following.

      After getting the result, the program should re-display the menu to allow users to continue using the application to select other tasks.

 

Example: if users select task 1, the output should be:

      CALCULATOR OF JAMES SMITH

      ADD TWO INTEGERS

      34 + 27 = 61

 

Example: if users select task 6, the output should be:

     CALCULATOR OF JAMES SMITH

     SUBTRACT TWO DECIMAL NUMBERS

     75.23 – 12.8 =  62.43

 

Remember:

-Change Smith to your last name

-Change JAMES SMITH  to your full name

-The file name should be the first comment line at the top of class

-write the comment on each parts of both classes

-get the output pictures of each tasks, paste them after the pseudo code

       

HOW TO TURN IN THE LAB

PART 1

DataTypeClass_yourLastName.java

DataTypeClass_yourLastName.class

DriverClass_yourLastName.java

DriverClass_yourLastName.class

File of pseudo-code, UML and output of part1

 

PART 2

MathOperation.java (downloaded from eCampus)

MathOperation.class

MathCalculator_yourLastName.java

MathCalculator_yourLastName.class

File of pseudo-code, UML and output of part 2

 

 

HOW TO GRADE THE LAB

COMPLETE LAB ON TIME

3

Part1: question 1

1

Part1: question 2

1

Part1: question 3

1

Part1: question 4

1

Part1: question 5

1

Part1: question 6

1

File of pseudo-code, UML, output

2

Compile success, pictures of output in correct format with your name

4

Comment: filename at top and on each part

1

Part2:

 

UML Pseudo-code or flowchart, pictures of output in correct format

2

Comment: filename at top and on each part

1

Compile success, qualified the requirement, output in correct format with your name

4

Manage menu to allow users to continue using program until exit

1

Read input

1

Create the object of class MathOperation

2

Display by using object to call method of class MathOperation

2

Results from calculation are correct

1

Total

30