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

java record unit 2

The document contains Java programming exercises focusing on the concept of inheritance, including the implementation of classes such as Circle, Cylinder, Person, Student, Staff, Author, and Book. It provides example code for creating objects and invoking methods, along with additional tasks related to string operations and checking for anagrams. The final result confirms the successful development and verification of the Java programs.

Uploaded by

717823l316
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)
2 views

java record unit 2

The document contains Java programming exercises focusing on the concept of inheritance, including the implementation of classes such as Circle, Cylinder, Person, Student, Staff, Author, and Book. It provides example code for creating objects and invoking methods, along with additional tasks related to string operations and checking for anagrams. The final result confirms the successful development and verification of the Java programs.

Uploaded by

717823l316
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/ 24

Page 1 of 24

Java Programming23CSR306

Ex no: 2
Inheritance
Date:

Aim:
To develop programs using the concept of Inheritance in Java.
Question 6:
Write a Java program to implement the following relationship and create a Main class to invoke
all the methods.

Code:

public class Circle {

private double radius = 1.0;

private String color = "red";

public Circle() {

Devadharshini . P 717823L310
Page 2 of 24
Java Programming23CSR306

public Circle(double radius) {

this.radius = radius;

public Circle(double radius, String color) {

this.radius = radius;

this.color = color;

public double getRadius() {

return radius;

public String getColor() {

return color;

public void setRadius(double radius) {

this.radius = radius;

public void setColor(String color) {

this.color = color;

public double getArea() {

return 3.14* radius * radius;

}
Devadharshini . P 717823L310
Page 3 of 24
Java Programming23CSR306

public String toString() {

return "Circle[radius=" + radius + ",color=" + color + "]";

public class Cylinder extends Circle {

private double height = 1.0;

public Cylinder() {

public Cylinder(double radius) {

super(radius);

public Cylinder(double radius, double height) {

super(radius);

this.height = height;

public Cylinder(double radius, double height, String color) {

super(radius, color);

this.height = height;

public double getHeight() {

return height;
Devadharshini . P 717823L310
Page 4 of 24
Java Programming23CSR306

public void setHeight(double height) {

this.height = height;

public double getVolume() {

return getArea() * height;

public String toString() {

return "Cylinder[" + super.toString() + ",height=" + height + "]";

public class Main {

public static void main(String[ ] args) {

Circle circle1 = new Circle();

Circle circle2 = new Circle(2.5);

Circle circle3 = new Circle(3.0, "blue");

System.out.println(circle1);

System.out.println(circle2);

System.out.println(circle3);

circle1.setRadius(4.5);

circle1.setColor("green");

System.out.println("Updated Circle1: " + circle1);


Devadharshini . P 717823L310
Page 5 of 24
Java Programming23CSR306

Cylinder cylinder1 = new Cylinder();

Cylinder cylinder2 = new Cylinder(2.5);

Cylinder cylinder3 = new Cylinder(2.5, 5.0);

Cylinder cylinder4 = new Cylinder(2.5, 5.0, "yellow");

System.out.println(cylinder1);

System.out.println(cylinder2);

System.out.println(cylinder3);

System.out.println(cylinder4);

cylinder1.setHeight(7.0);

System.out.println("Updated Cylinder1: " + cylinder1);

System.out.println("Volume of Cylinder3: " + cylinder3.getVolume());

System.out.println("Volume of Cylinder4: " + cylinder4.getVolume());

Devadharshini . P 717823L310
Page 6 of 24
Java Programming23CSR306

Output

Devadharshini . P 717823L310
Page 7 of 24
Java Programming23CSR306

Question 7:
Write a Java program to implement the following relationship and

create a Main class to invoke all the methods.

Aim:
To convert the UML diagram to code.
Code:
class Person {

private String name;

private String address;

public Person(String name, String address) {

this.name = name;

this.address = address;

Devadharshini . P 717823L310
Page 8 of 24
Java Programming23CSR306

public String getName() {

return name;

public String getAddress() {

return address;

public void setAddress(String address) {

this.address = address;

public String toString() {

return "Person[name=" + name + ", address=" + address + "]";

class Student extends Person {

private String program;

private int year;

private double fee;

public Student(String name, String address, String program, int year, double fee) {

super(name, address);

this.program = program;

this.year = year;

this.fee = fee;

public String getProgram() {

return program;

public void setProgram(String program) {

this.program = program;
Devadharshini . P 717823L310
Page 9 of 24
Java Programming23CSR306

public int getYear() {

return year;

public void setYear(int year) {

this.year = year;

public double getFee() {

return fee;

public void setFee(double fee) {

this.fee = fee;

public String toString() {

return "Student[" + super.toString() + ", program=" + program + ", year=" + year + ", fee=" + fee + "]";

class Staff extends Person {

private String school;

private double pay;

public Staff(String name, String address, String school, double pay) {

super(name, address);

this.school = school;

this.pay = pay;

public String getSchool() {

return school;
Devadharshini . P 717823L310
Page 10 of 24
Java Programming23CSR306

public void setSchool(String school) {

this.school = school;

public double getPay() {

return pay;

public void setPay(double pay) {

this.pay = pay;

public String toString() {

return "Staff[" + super.toString() + ", school=" + school + ", pay=" + pay + "]";

public class Main {

public static void main(String[] args) {

Person person = new Person("John", "123 Street");

System.out.println(person);

Student student = new Student("Alice", "456 Avenue", "ECE", 2, 5000.0);

System.out.println(student);

Staff staff = new Staff("Bob", "789 Boulevard", "XYZ School", 2000.0);

System.out.println(staff);

Devadharshini . P 717823L310
Page 11 of 24
Java Programming23CSR306

Output:

Devadharshini . P 717823L310
Page 12 of 24
Java Programming23CSR306

Question 8:
Write a Java program to implement the following relationship and create a Main class to invoke
all the methods.

Aim:
To convert the given UML diagram to code.
Code:
class Author {
private String name;
private String email;
private char gender;

public Author(String name, String email, char gender) {


this.name = name;
this.email = email;
this.gender = gender;
}

Devadharshini . P 717823L310
Page 13 of 24
Java Programming23CSR306

public String getName() {


return name;
}

public String getEmail() {


return email;
}

public char getGender() {


return gender;
}
public String toString() {
return "Author[name=" + name + ", email=" + email + ", gender=" + gender + "]";
}
}
class Book {
private String name;
private Author author;
private double price;
private int qty = 0;

public Book(String name, Author author, double price) {


this.name = name;
this.author = author;
this.price = price;
}

public Book(String name, Author author, double price, int qty) {


this.name = name;
this.author = author;

Devadharshini . P 717823L310
Page 14 of 24
Java Programming23CSR306

this.price = price;
this.qty = qty;
}

public String getName() {


return name;
}

public Author getAuthor() {


return author;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public int getQty() {


return qty;
}

public void setQty(int qty) {


this.qty = qty;
}
public String toString() {
return "Book[name=" + name + ", " + author + ", price=" + price + ", qty=" + qty + "]";
}
}

Devadharshini . P 717823L310
Page 15 of 24
Java Programming23CSR306

public class Main {


public static void main(String[] args) {
// Create an Author
Author author = new Author("George", "[email protected]", 'M');
System.out.println(author);
Book book1 = new Book("Java Programming", author, 59.99);
System.out.println(book1);
Book book2 = new Book("Advanced Java", author, 75.99, 10);
System.out.println(book2);
}
}

Devadharshini . P 717823L310
Page 16 of 24
Java Programming23CSR306

Output:

Devadharshini . P 717823L310
Page 17 of 24
Java Programming23CSR306

Question 9:
Let s1 be "Welcome" and s2 be "welcome". Write a Java program for
the following statements:
(a) Check whether s1 is equal to s2 and assign the result to a boolean
variable ‘isEqual’.
(b) Check whether s1 is equal to s2, ignoring case, and assign the result
to a boolean variable ‘isEqual’.
(c) Compare s1 with s2 and assign the result to an int variable x.
(d) Compare s1 with s2, ignoring case, and assign the result to an int
variable x.
(e) Check whether s1 has the prefix AAA and assign the result to a
boolean variable b.
(f) Check whether s1 has the suffix AAA and assign the result to a
boolean variable b.
(g) Assign the length of s1 to an int variable x.
(h) Assign the first character of s1 to a char variable x.
(i) Create a new string s3 that combines s1 with s2.
(j) Create a substring of s1 starting from index 1.
(k) Create a substring of s1 from index 1 to index 4.
(l) Create a new string s3 that converts s1 to lowercase.
(m) Create a new string s3 that converts s1 to uppercase.
(n) Create a new string s3 that trims whitespace characters on both ends
of s1.
(o) Assign the index of the first occurrence of the character e in s1 to
an int variable x.
(p) Assign the index of the last occurrence of the string abc in s1 to an
int variable x.
Aim:
To write a java program for the given statements.

Devadharshini . P 717823L310
Page 18 of 24
Java Programming23CSR306

Code:
public class StringOperations {
public static void main(String[] args) {

String s1 = "Welcome";
String s2 = "welcome";

boolean isEqual = s1.equals(s2);


System.out.println("s1 is equal to s2: " + isEqual);

boolean isEqualIgnoreCase = s1.equalsIgnoreCase(s2);


System.out.println("s1 is equal to s2 ignoring case: " + isEqualIgnoreCase);

int x = s1.compareTo(s2);
System.out.println("Comparison of s1 with s2: " + x);

int xIgnoreCase = s1.compareToIgnoreCase(s2);


System.out.println("Comparison of s1 with s2 ignoring case: " + xIgnoreCase);

boolean hasPrefix = s1.startsWith("AAA");


System.out.println("s1 has prefix AAA: " + hasPrefix);

boolean hasSuffix = s1.endsWith("AAA");


System.out.println("s1 has suffix AAA: " + hasSuffix);

int length = s1.length();


System.out.println("Length of s1: " + length);

char firstChar = s1.charAt(0);


System.out.println("First character of s1: " + firstChar);

Devadharshini . P 717823L310
Page 19 of 24
Java Programming23CSR306

String s3 = s1 + s2;
System.out.println("Combined string s3: " + s3);

String substringFromIndex1 = s1.substring(1);


System.out.println("Substring of s1 from index 1: " + substringFromIndex1);

String substringFrom1To4 = s1.substring(1, 5);


System.out.println("Substring of s1 from index 1 to 4: " + substringFrom1To4);

String s3LowerCase = s1.toLowerCase();


System.out.println("s1 in lowercase: " + s3LowerCase);

String s3UpperCase = s1.toUpperCase();


System.out.println("s1 in uppercase: " + s3UpperCase);

String s3Trimmed = s1.trim();


System.out.println("s1 trimmed: " + s3Trimmed);

int indexOfFirstE = s1.indexOf('e');


System.out.println("Index of first occurrence of 'e' in s1: " + indexOfFirstE);

int indexOfLastABC = s1.lastIndexOf("abc");


System.out.println("Index of last occurrence of 'abc' in s1: " + indexOfLastABC);
}
}

Devadharshini . P 717823L310
Page 20 of 24
Java Programming23CSR306

Output:

Devadharshini . P 717823L310
Page 21 of 24
Java Programming23CSR306

Question 10:
An anagram is a word or a phrase made by transposing the letters of another word or phrase; for
example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear
oft." Write a Java program that figures out whether one string is an anagram of another string.
The program should ignore white space and punctuation.
Aim:
To write a Java program that figures out whether one string is an anagram of another string.
Code:
package unit2;
import java.util.Arrays;
import java.util.Scanner;
public class AnagramMain {
public static boolean areAnagrams(String str1, String str2) {
String normalizedStr1 = str1.replaceAll("[\\W]", "").toLowerCase();
String normalizedStr2 = str2.replaceAll("[\\W]", "").toLowerCase();

if (normalizedStr1.length() != normalizedStr2.length()) {
return false;
}

char[] charArray1 = normalizedStr1.toCharArray();


char[] charArray2 = normalizedStr2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);

return Arrays.equals(charArray1, charArray2);


}

private static String normalizeString(String str) {


return str.replaceAll("[\\W]", "").toLowerCase();
}

Devadharshini . P 717823L310
Page 22 of 24
Java Programming23CSR306

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter string1:");
String string1 = sc.nextLine();
System.out.println("Enter string2:");
String string2 = sc.nextLine();
System.out.println("\"" + string1 + "\" and \"" + string2 + "\" are anagrams: " +
areAnagrams(string1, string2));
}
}

Devadharshini . P 717823L310
Page 23 of 24
Java Programming23CSR306

Output:

Devadharshini . P 717823L310
Page 24 of 24
Java Programming23CSR306

Result:
Thus, the Java programs using inheritance has been successfully developed and the output was verified.

Devadharshini . P 717823L310

You might also like