Professional Documents
Culture Documents
1
1
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");
}
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));
}
}
Lab 1
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:
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:
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
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:
Lab 1
Source code:
class Teacher{
String teacherName;
String subject;
void teacherDisplay() {
System.out.println("Teacher name: "+ teacherName);
System.out.println("Subject name: "+ subject);
}
}
void studentDisplay() {
System.out.println("Student name: "+ studentName);
System.out.println("Roll number: "+ rollNo);
}
}
Source code:
import static java.lang.Math.PI;
interface Shapes{
double Area();
}
Lab 1
Circle(double r){
this.radius = r;
}
@Override
public double Area() {
return PI * radius * radius;
}
}
public Square(double l) {
this.length = l;
}
@Override
public double Area() {
return length * length;
}
}
Source code:
abstract class Bike{
abstract void run();
}
Lab 1
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