OOP_Lab.docx
OOP_Lab.docx
01:
Experiment Name / Title: Write a Java program to print Text on screen and Assigned
variables to perform basic operation.
Java User Input (Scanner): The Scanner class is used to get user input, and it is found in
the java.util package. To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation. In our example, we will use
the nextLine() method, which is used to read Strings:
Input Types:
In the example above, we used the nextLine() method, which is used to read Strings. To read
other types, look at the table below:
Method Description
nextBoolean()Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Code: Ex-1
public class Exercise1 {
public static void main(String[] args) {
// Print a greeting message to the console
System.out.println("Hello\nAlexandra Abramov!");
}
}
Result: Hello
Alexandra Abramov!
Ex-2:
Sample solution using input from the user:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner input = new Scanner(System.in);
// Prompt the user to input their first name
System.out.print("Input your first name: ");
String fname = input.next();
// Prompt the user to input their last name
System.out.print("Input your last name: ");
String lname = input.next();
// Output a greeting message with the user's full name
System.out.println();
System.out.println("Hello \n" + fname + " " + lname);
}
}
Result:
Input your first name: James
Input your last name: Smith
Hello
James Smith
Ex-3: A Java program to print the sum of two numbers:
Sum: 50.
Ex- 4: Write a Java program that takes a number as input and prints its multiplication table up to
10.
import java.util.Scanner;
// Use a loop to generate and print the multiplication table for the input number
for (int i = 1; i <= 10; i++) {
// Calculate and print the result of n multiplied by i
System.out.println(n + "*" + i + " = " + (n * i));
}
}
}
Results: Input the Number:
6
6*1 = 6
6*2 = 12
6*3 = 18
6*4 = 24
6*5 = 30
6*6 = 36
6*7 = 42
6*8 = 48
6*9 = 54
6*10 = 60
Ex- 5: Write a Java program to print the area and perimeter of a rectangle.
public class Exercise13 {
public static void main(String[] strings) {
// Define constants for the width and height of the rectangle
final double width = 5.6;
final double height = 8.5;
Array:
An array is a group of similar typed variables that are referred to by a common name. Arrays of
any type can be created and may have one or more dimensions. A specific element in an array is
accessed by its index. The array is a simple type of data structure which can store primitive
variables or objects. For example, imagine if you had to store the result of six subjects we can do
it using an array. To create an array value in Java, you use the new keyword, just as you do to
create an object.
Defining and constructing one dimensional array
Code: Ex-1: Write a Java program to get a number and print whether it is greater .
int y = 18;
if (x > y) {
Result:
x is greater than y
Code: Ex-2: Write a Java program to get a number from the user and print whether it is positive
or negative.
Test Data
Input number: 35
Expected Output :
Number is positive
import java.util.Scanner;
public class Exercise1 {
if (input > 0)
{
System.out.println("Number is positive");
}
else if (input < 0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
}
}
Result:
Input number: 35
Number is positive
Ex-3: Write a Java program that takes a number from the user and generates an integer between
1 and 7. It displays the weekday name.
import java.util.Scanner;
public class Exercise5 {
System.out.println(getDayName(day));
}
return dayName;
}
}
Sample Output:
Input number: 3
Wednesday
Ex-4: Java Program to demonstrate the example of for loop which prints 1 to 10.
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
Result: 1 2 3 4 5 6 7 8 9 10
Ex-5: This program will help to understand initializing and accessing specific array elements:
import java.util.Arrays;
public class ResultListDemo {
public static void main(String[] args) {
//Array Declaration
int resultArray[] = new int[6];
//Array Initialization
resultArray[0]=69;
resultArray[1]=75;
resultArray[2]=43;
resultArray[3]=55;
resultArray[4]=35;
resultArray[5]=87;
//Array elements access
System.out.println("Marks of First Subject- "+ resultArray[0]);
System.out.println("Marks of Second Subject- "+ resultArray[1]);
System.out.println("Marks of Third Subject- "+ resultArray[2]);
System.out.println("Marks of Fourth Subject- "+ resultArray[3]);
System.out.println("Marks of Fifth Subject- "+ resultArray[4]);
System.out.println("Marks of Sixth Subject- "+ resultArray[5]);
}
}
Experiment No. 03:
Experiment Name / Title: To write a JAVA program to implement class mechanism.
Create a class, methods and invoke them inside main method
class:
a class describes the contents of the objects that belong to it: it describes an aggregate of
data fields (called instance variables), and defines the operations (called methods).
object:
an object is an element (or instance) of a class; objects have the behaviors of their class.
The object is the actual component of programs, while the class specifies how instances
are created and how they behave.
method:
a method is an action which an object is able to perform.
sending a message
sending a message to an object means asking the object to execute or invoke one of its
methods.
Ex-1:
class A
{
int l=10,b=20;
void display()
{
System.out.println(l);
System.out.println(b);
}
}
class method demo{
public static void main(String args[]){
A a1=new A();
a1.display();
}
}
Output:
10
20
Ex-3: Print Area of a circle and a rectangle by creating a ‘test’ class in Java.
package javalab52c5b;
public class test {
double pi;
double r;
double l;
void Circle()
{
System.out.print("Area of Circle is:");
System.out.println(pi*r*r);
}
void Rectangle()
{
System.out.print("Area of Rectangle is: ");
System.out.println(r*l);
}
}
package javalab52c5b;
public class Javalab52c5b {
public static void main(String[] args) {
test mytest1=new test();
test mytest2=new test();
mytest1.pi=3.14;
mytest1.r=5;
mytest2.r=5;
mytest2.l=4.5;
mytest1.Circle();
mytest2.Rectangle();
}
}
Ex-4: An example of parameterized method in Java
class Main {
// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}
// create a method
public static int square(int num) {
// return statement
return num * num;
}
Code:
Ex-1: Example of default constructor: In this example, we are creating the no-arg constructor in
the Bike class. It will be invoked at the time of object creation.
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
OutPut:
Bike is created
Ex-2: The default constructor is used to provide the default values to the object like 0, null, etc.,
depending on the type:
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
0 null
0 null
Ex-3: Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We
can have any number of parameters in the constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}
OutPut:
111 Ayan
222 Aryan
Theory:
Inheritance:
1. Inheritance in java is a mechanism in which one object acquires all the
properties and behaviors of parent object.
2. The class which inherits the properties of other is known as subclass
(derived class, child class) and the class whose properties are inherited is
known as superclass (base class, parent class).
Code:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Theory:
Abstraction:
- Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
- It shows only important things to the user and hides the internal details for example
sending sms, you just type the text and send the message. You don't know the
internal processing about the message delivery.
NB. Any class having abstract method must be declared as abstract class.
b) Abstract Method:
Code:
Ex-1: Example of abstract class that has abstract method
In this example, Bike the abstract class that contains only one abstract
method run. It implementation is provided by the Honda class.
Output:
running safely.
Ex-2:
Understanding the real scenario of abstract class
In this example, Shape is the abstract class, its implementation is provided by the Rectangle
and Circle classes. Mostly, we don't know about the implementation class (i.e. hidden to the
end user) and object of the implementation class is provided by the factory method.
A factory method is the method that returns the instance of the class. We will learn about the
factory method later.
In this example, if you create the instance of Rectangle class, draw () method of Rectangle
class will be invoked.