$29.99
Lab Assignment 5
Objective 1: Class & Objects – member variables and methods.
Problem Statement: Create a class to accept 2 integers from an user. Based on the user
preference the system should display the addition or multiplication of two numbers. Both the
numbers should be positive, if the user has entered a negative number then you need to ignore
the sign of the number.
Note: Click here to know more about Scanner class or how to use scanner class.
Source Code:
import java.util.Scanner;
public class UserInput {
private int number1;
private int number2;
public int getNumber1() {
return number1;
}
public int getNumber2() {
return number2;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public int addition(int num1, int num2) {
return num1 + num2;
}
public int multiplication(int num1, int num2) {
return num1 * num2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int userPreference;
Scanner sc = new Scanner(System.IN);
UserInput userInput = new UserInput();
System.OUT.println("Enter the first number");
userInput.setNumber1(sc.nextInt());
System.OUT.println("Enter the second number");
userInput.setNumber2(sc.nextInt());
System.OUT.println(
"Press 1 to display addition," + " 2 to
display multiplication and " + "3 to display the numbers");
userPreference = sc.nextInt();
if (userPreference == 1) {
int add;
int num1,num2;
num1 = userInput.getNumber1();
num2 = userInput.getNumber2();
add = userInput.addition(Math.ABS(num1),Math.ABS(num2) );
System.OUT.println("Addition of 2 numbers is " + add);
} else if (userPreference == 2) {
int multi;
int num1,num2;
num1 = userInput.getNumber1();
num2 = userInput.getNumber2();
multi = userInput.multiplication(Math.ABS(num1),Math.ABS(num2));
System.OUT.println("Multiplication of 2 numbers is " + multi);
} else if (userPreference == 3) {
System.OUT.println("Numer 1 is " +userInput.getNumber1());
System.OUT.println("Numer 2 is " +userInput.getNumber2());
System.OUT.println("Have a nice day.... Bye");
}
sc.close();
}
}
Output:
Explanation:
Import the java package Scanner for the interactive console:
import java.util.Scanner;
Define the member variables:
private int number1;
private int number2;
Setters to set the values of the member variables:
public int getNumber1() {
return number1;
}
public int getNumber2() {
return number2;
}
Getters to retrieve the values stored in the member variable:
public void setNumber1(int number1) {
this.number1 = number1;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
Right click on the java file goto Source -> Generate Getters and Setters. As shown below:
Member methods for addition and subtraction:
public int addition(int num1, int num2) {
return num1 + num2;
}
public int multiplication(int num1, int num2) {
return num1 * num2;
}
Create a reference variable of the scanner class and instantiate it to store the object of the same.
For taking input from the console.
Scanner sc = new Scanner(System.IN);
Create a reference variable of the class and instantiate it to store the object of the same:
UserInput userInput = new UserInput();
Accepting the values from the interactive console and storing it in member variable. Click
here to learn more about scanner:
userInput.setNumber1(sc.nextInt());
userInput.setNumber2(sc.nextInt());
Retrieving the values stored inside the member variable:
num1 = userInput.getNumber1();
num2 = userInput.getNumber2();
In order to ignore the sign and take only positive number as input we have used absolute
(Math.abs) function. “userInput.addition” function is used to call the member method for adding
the two numbers and result inside “add” variable.
add = userInput.addition(Math.ABS(num1),Math.ABS(num2) );
close function is used to close the connection.
sc.close();
Syntax:
There are four ways in which you can define a method of a class. Please find below the syntax,
based on the return type and the arguments which you have passed.
<access specifier> <return type> <method name>(arguments){
statement;
….
}
Objective 2: Class and Objects
Problem Statement: Consider the mobile description of a product available in the store.
Please find below the features of a phone:
1. Samsung Galaxy On Nxt:
• 3 GB RAM | 16 GB ROM | Expandable Upto 256 GB
• 5.5 inch Full HD Display
• 13MP Rear Camera | 8MP Front Camera
• 3300 mAh Li-ion Battery
• Price: Rs 11900
2. iVooMi Me4:
• 1 GB RAM | 8 GB ROM | Expandable Upto 64 GB
• 4.5 inch FWVGA Display
• 5MP Rear Camera | 5MP Front Camera
• 2000 mAh Battery
• Price: 2999
Create a class and object using java programming for the above mentioned data. Based on the
user input display the features of the product. Give appropriate names to the field
variables(camel casing)
Note: Click here to know more about Scanner class or how to use scanner class. You
need to generate getters and setters to set the values or to retrieve the values of an object. All
the member variables should be declared as private and methods should be declared as
public.
Objective 3: Class & Objects
Problem Statement: Mobile store admin wants to enter details of a new employee. Create
a java class which will contain the following fields: Employee ID, First name, Last name,
email-id,phone number, address, salary, gender, joining date and date of birth. Select
appropriate data type for the above fields. For simplicity you can store joining date and date
of birth as String.
Note: The admin should enter the details of the employee using interactive console. The
admin can enter only 1 employee at a time.
Click here to know more about Scanner class or how to use scanner class. You need to
generate getters and setters to set the values or to retrieve the values of an object. All the
member variables should be declared as private and methods should be declared as public.
Exercise on Methods:
Objective-1 Use of do–While, switch case, break and methods with return type and not
argument
Problem Statement: Write a Java program that takes a user choice to print addition,
subtraction, multiplication or division of two numbers (numbers are 5 and 10).
Source Code:
package lab5;
import java.util.Scanner;
public class ArthimeticOperation {
private int num1 = 5; private
int num2 = 10;
public int addition() {
return num1 + num2;
}
public int multiplication() {
return num1 * num2;
}
public int division() {
return num1 / num2;
}
public int subtraction() {
return num1 - num2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int choice;
do {
System.out.println("Help on:");
System.out.println(" 1. addition");
System.out.println(" 2. multiplication");
System.out.println(" 3. Subtraction");
System.out.println(" 4. division");
System.out.println("Choose one:");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
} while (choice < 1 || choice > 4);
ArthimeticOperation operation =
new ArthimeticOperation();
int value;
switch (choice) {
case 1:
value = operation.addition();
System.out.println("Addition of two numbers
is " + value);
break;
case 2:
value = operation.multiplication();
System.out.println("multiplication of two
numbers is " + value);
break;
case 3:
value = operation.subtraction();
System.out.println("subtraction of two
numbers is " + value);
break;
case 4:
value = operation.division();
System.out.println("division of two numbers
is " + value);
break;
}
}
}
Output:
Explanation: First we need to create a class with 2 variables and initialize them with 5 and
10.
public class ArthimeticOperation {
private int num1 = 5;
private int num2 = 10;
}
Now we need to create 4 methods which can add, multiply, subtract and divide these
two variables.
public int addition() {
return num1 + num2;
}
public int multiplication() {
return num1 * num2;
}
public int division() {
return num1 / num2;
}
public int subtraction() {
return num1 - num2;
}
All of the above methods will return the value back to the calling function. Return type of
each variable is int and these functions are not accepting any parameters since we have
already initialized them as the instance variable.
Now, we need to accept the user choice. For, we are using do-while loop inside the main
method, so that at least once it will enter inside the loop and execute the
statements present inside the block. Since the do while loop will first execute the block
and then it will check the condition. Which is not possible with the while loop.
do {
System.out.println("Help on:");
System.out.println(" 1. addition");
System.out.println(" 2. multiplication");
System.out.println(" 3. Subtraction");
System.out.println(" 4. division");
System.out.println("Choose one:"); Scanner
sc = new Scanner(System.in); choice =
sc.nextInt();
} while (choice < 1 || choice > 4);
In the above code if the choice is less than 1 and greater than 4, then the system will
again ask the user to enter the choice.
Now, based on the choice from the user we have created a switch case statement.
switch (choice) {
case 1:
value = operation.addition();
System.out.println("Addition of two numbers
is " + value);
break;
case 2:
value = operation.multiplication();
System.out.println("multiplication of two
numbers is " + value);
break;
case 3:
value = operation.subtraction();
System.out.println("subtraction of two
numbers is " + value);
break;
case 4:
value = operation.division();
System.out.println("division of two numbers
is " + value);
break;}
operation.addition() method will invoke the addition method which will sum up the
two numbers and return the value to the place from where it has been called. This
function returns a int value which is stored inside value variable.
value = operation.addition();
If we do not use break after each case then it will execute the next case as well for
example.
switch (choice) {
case 1:
value = operation.addition();
System.out.println("Addition of two numbers
is " + value);
case 2:
value = operation.multiplication();
System.out.println("multiplication of two
numbers is " + value);
case 3:
value = operation.subtraction();
System.out.println("subtraction of two
numbers is " + value);
case 4:
value = operation.division();
System.out.println("division of two numbers
is " + value);
break;
}
In the above code if the user preference is choice 1 then it will execute case 1,
case 2, case 3 and case 4.
If the user choice is case 3, then it will execute case 3 and case 4.
Objective-2 Different type of methods present in java.
Problem Statement: Write a java program to create methods mentioned below:
Note: The functions should be in a different class and main function should be in another
class.
\} Generate a Fibonnical series and print the value present at the 15th position (Method
name: fib())
Note: Method should not have any parameter, but it should return a value.
\} Find out how many prime numbers are present between 0 and 1000 and print the total
count of prime numbers.(Method name: prime())
Note: Method should have parameters (the initial and the final range) and a return
type
\} Find out the factorial of a given number (Method name: fact())
Note: Method should have a parameter, but it should not have any return value
\} Print the first 20 Armstrong number (Method name: arm())
Note: Method should not have any parameter nor any return value
Based on the user preference the program should perform the above mentioned task.