1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab 1

1) An array is called balanced if it’s even numbered elements (a[0], a[2], etc.) are even and its
odd numbered elements (a[1], a[3], etc.) are odd. Write a function named balanced that
accepts an array of integers which returns 1 if the array is balanced and returns 0 otherwise.
[2075]

Source code:
public class BalancedArray {
public static void main(String[] args) {
int x[] = {0,1,2,3,4,5,6,7,8};
int result = balanced(x);
if(result == 1)
System.out.println("Array is balanced");
else
System.out.println("Array is not balanced");
}

static int balanced(int[] a) {


int c = 0;
for(int i=0; i<a.length; i++) {
if ((a[i]%2) == (i%2)) {
c++;
}
}
if(c == a.length)
return 1;
else
return 0;
}
}
Output:

2) Write an object oriented program to find area and perimeter of rectangle. [2073, 2074]

Source code:
class Rectangle{
int length, breadth;
public Rectangle(int l, int b) {
length = l;
breadth = b;
}
public int area() {
return (length * breadth);
}
public int perimeter() {
return (2 * (length + breadth));
}
}

public class AreaAndPerimeter {


public static void main(String[] args) {
Rectangle r = new Rectangle(5,15);


































Lab 1

System.out.println("Area: " + r.area());


System.out.println("Perimeter: " + r.perimeter());
}
}
Output:

3) Write a program to input and add two numbers using static methods (procedural
programming).

Source code:
public class AddTwoNo {
static double sum(double a, double b) {
return (a + b);
}
public static void main(String[] args) {
System.out.println("Sum: " + sum(14, 2.30));
}
}
Output:

4) Write a program to input principle, time and rate, then calculate simple interest using static
methods.

Source code:
import java.util.Scanner;
public class SimpleInterestClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter principle: ");
double p = in.nextDouble();
System.out.print("Enter rate: ");
double r = in.nextDouble();
System.out.print("Enter time: ");
double t = in.nextDouble();
double i = (p * t * r) / 100;
System.out.println("Interest: " + i);
}
}
Output:




















Lab 1

5) Write both procedural and object oriented programs to calculate the area of a

a) Circle

b) Square

c) Rectangle

d) Sphere

Source code:
class Circle{
double radius;
Circle(double r){
radius = r;
}
public void area() {
double a = 2 * Math.PI * radius * radius;
System.out.println("Area of circle: " + a);
}
}
class Sphere{
double radius;
Sphere(double r){
radius = r;
}
public void area() {
double a = 4 * Math.PI * radius * radius;
System.out.println("Area of sphere: " + a);
}
}
class Rectangles{
double length, breadth;
public Rectangles(double l, double b) {
length = l;
breadth = b;
}
public void area() {
double a = length * breadth;
System.out.println("Area of rectangle: " + a);
}
}
class Square{
double length;
public Square(double l) {
length = l;
}
public void area() {
double a = length * length;
System.out.println("Area of square: " + a);
}

































Lab 1

}
public class AreaOfShapes {
public static void main(String[] args) {
Circle c = new Circle(3.4);
c.area();
Sphere s = new Sphere(23.4);
s.area();
Rectangles r = new Rectangles(23,5);
r.area();
Square sq = new Square(3.5);
sq.area();
}
}
Output:

6) Write a static method to calculate the sum of a one dimensional array.

Source code:
public class SumOfArray {
public static void main(String[] args) {
int a[] = {1,2,3,4,5,6,7,6,4,21,4,4,4,2};
int sum = 0;
for(int i: a)
sum += i;
System.out.println("Sum of array: " + sum);
}
}
Output:

7) Write a static method to calculate the average of a one dimensional array.

Source code:
public class AverageOfArray {
public static void main(String[] args) {
int a[] = {1,2,3,4,5,6,7,6,4,21,4,4,4,2};
double sum = 0;
for(int i: a)
sum += i;
double average = sum / a.length;
System.out.println("Average of array: " + average);
}
}
Output:

























Lab 1

8) Write a program to demonstrate encapsulation.

Source code:

Encapsulation.java
public class Encapsulation {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String collegeName;
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
private int phone;
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
Encapsulation2.java
public class Encapsulation2 {
public static void main(String[] args) {
Encapsulation student = new Encapsulation();

student.setName("Saajan Shrestha");
student.setCollegeName("Trinity");
student.setPhone(1234);

System.out.println(student.getName());
System.out.println(student.getCollegeName());
System.out.println(student.getPhone());
}
}
Output:

9) Write a program to demonstrate inheritance.


































Lab 1

Source code:
class Teacher{
String teacherName;
String subject;

void teacherDisplay() {
System.out.println("Teacher name: "+ teacherName);
System.out.println("Subject name: "+ subject);
}
}

class Student extends Teacher{


String studentName;
int rollNo;

void setData(String student, String teacher, String sub, int roll) {


studentName = student;
teacherName = teacher;
subject = sub;
rollNo = roll;
}

void studentDisplay() {
System.out.println("Student name: "+ studentName);
System.out.println("Roll number: "+ rollNo);
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Student studentOne = new Student();
studentOne.setData("Saajan Shrestha", "Sumanta Silwal", "Java",
32);
studentOne.teacherDisplay();
studentOne.studentDisplay();
}
}
Output:

10) Write a program to demonstrate polymorphism using interface as parent.

Source code:
import static java.lang.Math.PI;

interface Shapes{
double Area();
}

class Circle implements Shapes{


double radius;






























Lab 1

Circle(double r){
this.radius = r;
}

@Override
public double Area() {
return PI * radius * radius;
}
}

class Square implements Shapes{


double length;

public Square(double l) {
this.length = l;
}

@Override
public double Area() {
return length * length;
}
}

public class PolymorphismWithInterface {

public static void main(String[] args) {


Shapes shapeOne = new Circle(5.5);
Shapes shapeTwo = new Square(10.5);

System.out.println("Area of circle: " + shapeOne.Area());


System.out.println("Area of square: " + shapeTwo.Area());
}
}
Output:

11) Write a program to demonstrate polymorphism using abstract class as parent.

Source code:
abstract class Bike{
abstract void run();
}

class Honda extends Bike{


void run() {
System.out.println("Honda is running!!!");
}
}

class Yamaha extends Bike{


void run() {
System.out.println("Yamaha is running!!!");
}
}































Lab 1

public class PolymorphismDemo {


public static void main(String[] args) {
Bike one = new Honda();
Bike two = new Yamaha();

one.run();
two.run();
}
}
Output:

12) Write a program to create two classes Circle and Square, with appropriate fields and
methods, in a package name shape. Create a separate class ShapeDemo to test the classes.

Source code:

Square.java
package shape;
public class Square {
double length;
public Square(double l) {
this.length = l;
}
public double Area() {
return length * length;
}
}
Circle.java
package shape;
import static java.lang.Math.PI;
public class Circle {
double radius;
public Circle(double r){
this.radius = r;
}
public double Area() {
return PI * radius * radius;
}
}
ShapeDemo.java
package shape;
public class ShapeDemo {
public static void main(String[] args) {
Circle circle = new Circle(5.2);
Square square = new Square(11.2);
System.out.println("Area of circle: " + circle.Area());
System.out.println("Area of square: " + square.Area());
}
}
Output:



























Lab 1

You might also like