DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Ex no:3.1 PROGRAMS USING PACKAGES, ABSTRACT CLASSES AND
INTERFACES
Date:
QUESTION:
Write a Java program to implement the following relationship and create a Main class to invoke all the
methods.
AIM:
To write a program that implement data abstraction among shape, circle, rectangle and square classes.
CODE:
Shape.java
package Lab;
public abstract class Shape {
protected String color;
protected boolean
filled; public Shape() {
this.color="red"
;
this.filled=true;
}
public Shape(String color,boolean filled) {
this.color=color; this.filled=filled;
}
public String getColor() {
return color;
}
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
public void setColor(String color)
{ this.color=color;
}
public void setFilled(boolean filled) {
this.filled=filled;
}
public boolean filled() {
return filled;
}
public abstract double getArea();
public abstract double getPerimeter();
public String toString() {
return "Shape [ color="+color+",filled="+filled+"]";
}
}
Circle.java
package Lab;
public class Circle extends Shape{
protected double radius; public Circle() {
radius=0.1;
}
public Circle(double radius) {
this.radius=radius;
}
public Circle(double radius ,String color,boolean filled) {
super(color,filled); this.radius=radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius=radius;
}
@Override
public double getArea() {
return Math.PI*radius*radius;
}
@Override
public double getPerimeter() {
return 2*Math.PI*radius;
}
@Override
public String toString() {
return "Circle [ radius="+radius+","+super.toString()+" ] ";
}
}
Rectangle.java
package Lab;
public class Rectangle extends Shape{
protected double width;
protected double length;
public Rectangle() {
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
width=1.0;
length=1.0
;
}
public Rectangle(double width,double length)
{ this.width=width;
this.length=length;
}
public Rectangle(double width,double length,String color,boolean filled) {
super(color,filled);
this.width=width;
this.length=length
;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width=width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length=length;
}
@Override
public double getArea() {
return length*width;
}
@Override
public double getPerimeter() {
return 2*(length+width);
}
@Override
public String toString() {
return "Rectangle [ width="+width+",length ="+length+","+super.toString()+" ] ";
}
}
Square.java
package Lab;
public class Square extends Rectangle
{ private double side;
public Square() {
side=1.0;
}
public Square(double side) {
super(side,side);
}
public Square(double side,String color,boolean filled)
{ super(side,side,color,filled);
}
public double getSide() {
return super.getLength();
}
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
public void setSide(double side) {
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
super.setLength(side)
;
super.setWidth(side);
}
public void setWidth(double side) {
super.setWidth(side);
}
public void setLength (double side) {
super.setLength(side);
}
@Override
public String toString() {
return "Square [ side="+side+","+super.toString() +" ] ";
}
}
TestShape.java
package Lab;
public class TestShape {
public static void main(String[] args) {
Circle c =new Circle
(2.4,"Purple",true);
System.out.println(c.toString());
Rectangle r=new Rectangle(2.5,1.3,"Red",false);
System.out.println(r.toString());
Square s=new Square(1.4,"Blue",true);
System.out.println(s.toString());
}
}
OUTPUT:
RESULT:
Thus, the Java Program has been successfully implemented and the output was verified.
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Ex no: 3.2 PROGRAMS USING PACKAGES, ABSTRACT CLASSES AND
INTERFACES
Date:
QUESTION:
Identify the relationship between the classes and write a Java program to implement the relationship.
Create atest class to calculate the Total Rent, Total Rent= ((days*rentalrate)/7)+charge.
Hint:
1) Rental rate should be assigned based on cabin number. If the cabin number is 1or 2 or 3 then the
rental rate is rs.950per week. Otherwise Rs.1100 per week.
2) Set HolidayCabinRental Charge as Rs.150 and RegularCabinRental as Rs.100.
3) use getter and setter method wherever necessary.
AIM:
To write a program to implement data abstraction between the given classes.
CODE:
CabinRental.java
package Lab;
public abstract class CabinRental {
protected int cabinNo;
protected double rentalRate;
public CabinRental(int cabinNo)
{ this.cabinNo = cabinNo;
if (cabinNo == 1 || cabinNo == 2 || cabinNo == 3) {
this.rentalRate = 950.0;
}
else
{ this.rentalRate = 1100.0;
}
}
public abstract double calculateRent(int days);
public int getCabinNo() {
return cabinNo;
}
public double getRentalRate() {
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
return rentalRate;
}
}
HolidayCabinRental.java
package Lab;
public class HolidayCabinRental extends CabinRental {
private double charge;
public HolidayCabinRental(int cabinNo, double charge) {
super(cabinNo); this.charge = charge;
}
@Override
public double calculateRent(int days) {
return ((days * rentalRate) / 7) + charge;
}
public double getCharge() {
return charge;
}
public void setCharge(double charge) {
this.charge = charge;
}
@Override
public String toString() {
return "HolidayCabinRental [ cabinNo=" + cabinNo + ", rentalRate=" + rentalRate + ", charge=" +
charge + " ] ";
}
}
RegularCabinRental.java
package Lab;
public class RegularCabinRental extends CabinRental {
private double charge;
public RegularCabinRental(int cabinNo, double charge) {
super(cabinNo);
this.charge = charge;
}
@Override
public double calculateRent(int days) {
return ((days * rentalRate) / 7) + charge;
}
public double getCharge() {
return charge;
}
public void setCharge(double charge) {
this.charge = charge;
}
@Override
public String toString() {
return "RegularCabinRental [ cabinNo=" + cabinNo + ", rentalRate=" + rentalRate + ", charge="
+ charge + " ] ";
}
}
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
TestRent.java
package Lab;
public class TestRent {
public static void main(String[] args)
{ double holidayCharge =
150.0; double regularCharge =
100.0;
CabinRental holidayRental = new HolidayCabinRental(2,
holidayCharge); CabinRental regularRental = new RegularCabinRental(4,
regularCharge); int days = 10;
System.out.println(holidayRental);
System.out.println("Holiday Cabin Total Rent for " + days + " days: Rs. " +
holidayRental.calculateRent(days));
System.out.println(regularRental);
System.out.println("Regular Cabin Total Rent for " + days + " days: Rs. " +
regularRental.calculateRent(days));
}
}
OUTPUT:
RESULT:
Thus, the Java Program has been successfully implemented and the output was verified.
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Ex No:3.3 PROGRAMS USING PACKAGES, ABSTRACT CLASSES AND
INTERFACES
Date:
QUESTION:
Write a Java program to implement the following relationship and create a Main class to invoke all the
methods.
AIM:
To write a program to implement interface between the given classes.
CODE:
Movable.java
package Lab;
interface Movable {
public void moveUp();
public void moveDown();
public void moveLeft();
public void moveRight();
}
MovablePoint.java
package Lab;
public class MovablePoint implements Movable
{ int x, y, xSpeed, ySpeed;
public MovablePoint(int x, int y, int xSpeed, int ySpeed)
{ this.x = x;
this.y = y;
this.xSpeed =
xSpeed; this.ySpeed
= ySpeed;
}
@Override
public void moveUp() {
y += ySpeed;
}
@Override
public void moveDown() {
y -= ySpeed;
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
}
@Override
public void moveLeft() {
x -= xSpeed;
}
@Override
public void moveRight() {
x += xSpeed;
}
@Override
public String toString() {
return "MovablePoint [ " + "x=" + x + ", y=" + y + ", xSpeed=" + xSpeed +", ySpeed=" +
ySpeed + " ] ";
}
}
MovableCircle.java
package Lab;
public class MovableCircle implements Movable {
private int radius;
private MovablePoint center;
public MovableCircle(int x, int y, int xSpeed, int ySpeed, int radius) {
this.radius = radius;
this.center = new MovablePoint(x, y, xSpeed, ySpeed);
}
@Override
public void moveUp() {
center.moveUp();
}
@Override
public void moveDown() {
center.moveDown()
;
}
@Override
public void moveLeft() {
center.moveLeft()
;
}
@Override
public void moveRight() {
center.moveRight()
;
}
@Override
public String toString() {
return "MovableCircle [ " +"radius=" + radius +", center=" + center + " ] ";
}
}
TestMovable.java
package Lab;
public class TestMovable {
public static void main(String[] args) {
MovablePoint point = new MovablePoint(0, 0, 5, 10);
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
System.out.println(point);
point.moveUp();
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
point.moveLeft();
System.out.println(point)
;
MovableCircle circle = new MovableCircle(0, 0, 5, 10, 15);
System.out.println(circle);
circle.moveDown();
circle.moveRight();
}} System.out.println(circle)
;
OUTPUT
:
RESULT:
Thus, the Java Program has been successfully implemented and the output was verified.
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Ex no: 4.1 PROGRAM USING EXCEPTION HANDLING
MECHANISM
Date:
QUESTION:
Write Java programs to handle the following exceptions:
a. InputMismatchException
b. NumberFormatException
c. ArrayIndexOutofBoundsException
d. NullPointerException
AIM:
To write a program to handle the exceptions.
CODE:
a. InputMismatchException
package Lab;
import java.util.InputMismatchException;
import java.util.Scanner;
public class InputMismatch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
System.out.println("You entered: " + num);
}
catch (InputMismatchException e) {
System.out.println("InputMismatchException caught: Please enter a valid integer.");
}
scanner.close();
}
}
OUTPUT:
b. NumberFormatException
package Lab;
import java.util.Scanner;
public class NumberFormat {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str = scanner.nextLine() ;
try {
int num = Integer.parseInt(str);
System.out.println("Parsed number: " + num);
}
catch (NumberFormatException e) {
System.out.println("NumberFormatException caught: Invalid number format.");
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
}
scanner.close();
}
}
OUTPUT:
OUTPUT:
d. NullPointerException
package Lab;
public class NullPointer {
public static void main(String[] args) {
String str = null;
try {
System.out.println(str.length());
}
catch (NullPointerException e)
{
System.out.println("NullPointerException caught: Attempt to use a null object.");
}
}
}
OUTPUT:
RESULT:
Thus, the Java Program has been successfully implemented and the output was verified.
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Ex no: 4.2
PROGRAM USING EXCEPTION HANDLING
Date: MECHANISM
QUESTION:
Write a Java program based on the following statements:
• Create a CourseException class that extends Exception and whose constructor receives a String
that holds a college course’s department (for example, CIS), a course number (for example, 101), and
a number of credits (for example, 3). Save the file as CourseException.java.
• Create a Course class with the same fields and whose constructor requires values for each
field. Upon construction,throw a CourseException if the department does not consist of three letters,
if the course number does notconsist of three digits between 100 and 499 inclusive, or if the credits
are less than 0.5 or more than 6. Save the class as Course.java.
• Write an application that establishes an array of at least six Course objects with valid and
invalid values. Display anappropriate message when a Course object is created successfully and
when one is not.
AIM:
To write a Java code to create an application for the given exception.
CODE:
CourseException.java
package Lab;
public class CourseException extends Exception {
public CourseException(String department, int courseNumber, double credits, String message) {
super("CourseException: " + message + " [Department: " + department + ", Course Number: " +
courseNumber + ", Credits: " + credits + "]");
}
}
Course,java
package Lab;
public class Course {
private String department;
private int courseNumber;
private double credits;
public Course(String department, int courseNumber, double credits) throws CourseException
{ if (department.length() != 3 || !department.matches("[A-Za-z]{3}")) {
throw new CourseException(department, courseNumber, credits, "Invalid department
format.");
}
if (courseNumber < 100 || courseNumber > 499) {
throw new CourseException(department, courseNumber, credits, "Invalid course
number.");
}
if (credits < 0.5 || credits > 6) {
throw new CourseException(department, courseNumber, credits, "Invalid number of
credits.");
}
this.department = department; this.courseNumber = courseNumber; this.credits = credits;
}
@Override
public String toString() {
return "Course [Department: " + department + ", Course Number: " + courseNumber + ",
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Credits: " + credits + "]";
}
}
TestCourse.java
package Lab;
public class TestCourse {
public static void main(String[] args) {
Course[] courses = new Course[6];
String[][] courseData = { {"DAA", "301", "3"},{"JAVA", "302", "4"},{"DBMS", "303",
"3"},{"OS", "304", "7"},{"CP","101", "3"}, {"FWD", "105", "3"} };
for (int i = 0; i < courseData.length; i++) {
try {
String department = courseData[i][0];
int courseNumber = Integer.parseInt(courseData[i][1]);
double credits = Double.parseDouble(courseData[i][2]);
courses[i] = new Course(department, courseNumber,
credits); System.out.println("Successfully created: " +
courses[i]);
}
catch (CourseException e) {
System.out.println(e.getMessage())
;
}
catch (NumberFormatException e) {
System.out.println("Error parsing course number or credits: " + e.getMessage());
}
}
}
}
OUTPUT:
RESULT:
717823P103
DEPARTMENT OF COMPUTER SCIENCE AND 23CSR306 – JAVA
ENGINEERING PROGRAMMING
Thus, the Java Program has been successfully implemented and the output was verified.
717823P103