0% found this document useful (0 votes)
39 views

Oops With Java

Uploaded by

Garvit Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Oops With Java

Uploaded by

Garvit Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OOPS with JAVA

PRACTICAL FILE

Submitted By: Submitted To:


Name: Garvit Garg Dr. Neeta Sing
Enrolment No: 05119051722 Professor
Branch: IIOT-B1 USAR
INDEX

Sno. Date Title Techer’s


Signature
LAB-1
Aim:
(A) Write a Program to print “Hello World” using command prompt.
Code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Try programiz.pro");
}
}

Output:

(B) Write a Program to calculate the area of rectangle using command prompt.

Code:
Public static void main(String[] args){
int length =30;
int bredth =40;
int area = length*breadth;
System.out.println(“Area of the rectangle is:”+area);
}
}

Output:
LAB-2
Aim:
(A) Write a Program to find whether the number is prime number or not using
‘for loop’.
Code:
public class Main {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}

Output:

(B) Write a Program to find whether the number is prime number or not using
‘while loop’.
Code:
public class Main {
public static void main(String[] args) {
int num = 33, i = 2;
boolean flag = false;
while (i <= num / 2) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output:
Lab-3
Aim:
(A) Arithmetic operators
Code:
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}

Output:

(B) Java Assignment Operators


Code:
class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// assign value using =
var = a;
System.out.println("var using =: " + var);
// assign value using =+
var += a;
System.out.println("var using +=: " + var);
// assign value using =*
var *= a;
System.out.println("var using *=: " + var);
}
}

Output:

(C) Relational Operators


Code:
class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}

Output:

(D) Java Unary Operators


Code:
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}

Output:
Lab-4
Aim: Write a program to print the star, Number and Character pattern
using Java packages
(I) Star Pattern:
(I.A) Pyramidal Pattern
Code:
public class PyramidPattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row = 6;
//Outer loop work for rows
for (i=0; i<row; i++)
{
//inner loop work for space
for (j=row-i; j>1; j--)
{
//prints space between two stars
System.out.print(" ");
}
//inner loop for columns
for (j=0; j<=i; j++ )
{
//prints star
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println();
}
}
}
OUTPUT:

(I.B) Dimond Pattern


Code:
import java.util.Scanner;
public class DiamondPattern
{
public static void main(String args[])
{
int row, i, j, space = 1;
System.out.print("Enter the number of rows you want to print: ");
Scanner sc = new Scanner(System.in);
row = sc.nextInt();
space = row - 1;
for (j = 1; j<= row; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= row - 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (row - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:

(I.C) Sandglass Star Pattern


Code:
import java.util.Scanner;
public class SandglassPattern
{
public static void main(String[] args)
{
int i, j, k, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows you want to print: ");
n = sc.nextInt();
for (i= 0; i<= n-1 ; i++)
{
for (j=0; j<i; j++)
{
System.out.print(" ");
}
for (k=i; k<=n-1; k++)
{
System.out.print("*" + " ");
}
System.out.println("");
}
for (i= n-1; i>= 0; i--)
{
for (j=0; j<i; j++)
{
System.out.print(" ");
}
for (k=i; k<=n-1; k++)
{
System.out.print("*" + " ");
}
System.out.println("");
}
sc.close();
}
}
OUTPUT:

(I.C) Right Pascal's Triangle Pattern


Code:
import java.util.Scanner;
public class RightPascalTrianglePattern
{
public static void main(String[] args)
{
int i, j, rows;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows you want to print: ");
rows = sc.nextInt();
for (i= 0; i<= rows-1; i++)
{
for (j=0; j<=i; j++)
{
System.out.print("*"+ " ");
}
System.out.println("");
}
for (i=rows-1; i>=0; i--)
{
for(j=0; j <= i-1;j++)
{
System.out.print("*"+ " ");
}
System.out.println("");
}
}
}
OUTPUT:

(II) Number Patterns:


(II.A) incremental Number Pattern
Code:
public class Pattern1
{
public static void main(String args[])
{
int i, j,number, n=7;
//loop for rows
for(i=0; i<n; i++)
{
number=1;
//loop for columns
for(j=0; j<=i; j++)
{
//prints num
System.out.print(number+ " ");
//incrementing the value of number
number++;
}
//throws the cursor at the next line after printing each row
System.out.println();
}
}
}
OUTPUT:
(II.B) K-shape Number Pattern
Code:
public class Pattern2
{
public static void main(String[] args)
{
int i, j, rows=9;
//Prints upper half pattern
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Prints lower half pattern
for (i = 2; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT:

(II.C) Triangular Number Pattern


Code:
import java.util.*;
public class Pattern3
{
public static void main(String[] args)
{
int i, j, rows;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows you want to print: ");
rows = sc.nextInt();
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
OUTPUT:

(II.D) Pyramidal Number Pattern


Code:
public class Pattern4
{
public static void main(String[] args)
{
for (int i = 1; i <= 4; i++)
{
int n = 8;
for (int j = 1; j<= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--)
{
int n = 10;
for (int j = 0; j<= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
}
}
OUTPUT:

(III) Number Patterns:


(III.A) Right Triangle Alphabetic Pattern
Code:
public class RightAlphabaticPattern
{
public static void main(String[] args)
{
int alphabet = 65; //ASCII value of capital A is 65
//inner loop for rows
for (int i = 0; i <= 8; i++)
{
//outer loop for columns
for (int j = 0; j <= i; j++)
{
//adds the value of j in the ASCII value of A and prints the corresponding alphabet
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
}
}
OUTPUT:
(III.B) Repeating Alphabet Pattern
Code:
public class RepeatingPattern
{
public static void main(String[] args)
{
int letter = 65; //ASCII value of capital A is 65
//inner loop for rwos
for (int i = 0; i<= 9; i++)
{
//outer loop for columns
for (int j = 0; j <= i; j++)
{
//prints the character
System.out.print((char) letter + " ");
}
letter++;
System.out.println();
}
}
}
OUTPUT:

(III.C) K-shape Alphabet Pattern


Code:
public class KshapePattern
{
public static void main(String[] args)
{
for (int i = 8; i >= 0; i--)
{
int alphabet = 65;
for (int j = 0; j <= i; j++)
{
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
for (int i = 0; i<= 8; i++)
{
int alphabet = 65;
for (int j = 0; j <= i; j++)
{
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
}
}
OUTPUT:

(III.D) Triangle Character Pattern


Code:
public class TriangleCharacterPattern
{
public static void main(String[] args)
{
for (int i = 0; i <= 8; i++)
{
int alphabet = 65;
for (int j = 8; j > i; j--)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print((char) (alphabet + k) + " ");
}
System.out.println();
}
}
}
OUTPUT:
Lab-5
Aim: Write a program for java Inheritance
(A) Inheritance:
Code:
class Animal {
// field and method of the parent class
String name;
public void eat() {
System.out.println("I can eat");
}
}
// inherit from Animal
class Dog extends Animal {
// new method in subclass
public void display() {
System.out.println("My name is " + name);
}
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// access field of superclass
labrador.name = "Rohu";
labrador.display();
// call method of superclass
// using object of subclass
labrador.eat();
}
}
OUTPUT:

(B) Method Overriding in Java Inheritance:


Code:
class Animal {
// method in the superclass
public void eat() {
System.out.println("I can eat");
}
}
// Dog inherits Animal
class Dog extends Animal {
// overriding the eat() method
@Override
public void eat() {
System.out.println("I eat dog food");
}
// new method in subclass
public void bark() {
System.out.println("I can bark");
}
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// call the eat() method
labrador.eat();
labrador.bark();
}
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy