java record unit 2
java record unit 2
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 Circle() {
Devadharshini . P 717823L310
Page 2 of 24
Java Programming23CSR306
this.radius = radius;
this.radius = radius;
this.color = color;
return radius;
return color;
this.radius = radius;
this.color = color;
}
Devadharshini . P 717823L310
Page 3 of 24
Java Programming23CSR306
public Cylinder() {
super(radius);
super(radius);
this.height = height;
super(radius, color);
this.height = height;
return height;
Devadharshini . P 717823L310
Page 4 of 24
Java Programming23CSR306
this.height = height;
System.out.println(circle1);
System.out.println(circle2);
System.out.println(circle3);
circle1.setRadius(4.5);
circle1.setColor("green");
System.out.println(cylinder1);
System.out.println(cylinder2);
System.out.println(cylinder3);
System.out.println(cylinder4);
cylinder1.setHeight(7.0);
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
Aim:
To convert the UML diagram to code.
Code:
class Person {
this.name = name;
this.address = address;
Devadharshini . P 717823L310
Page 8 of 24
Java Programming23CSR306
return name;
return address;
this.address = address;
public Student(String name, String address, String program, int year, double fee) {
super(name, address);
this.program = program;
this.year = year;
this.fee = fee;
return program;
this.program = program;
Devadharshini . P 717823L310
Page 9 of 24
Java Programming23CSR306
return year;
this.year = year;
return fee;
this.fee = fee;
return "Student[" + super.toString() + ", program=" + program + ", year=" + year + ", fee=" + fee + "]";
super(name, address);
this.school = school;
this.pay = pay;
return school;
Devadharshini . P 717823L310
Page 10 of 24
Java Programming23CSR306
this.school = school;
return pay;
this.pay = pay;
return "Staff[" + super.toString() + ", school=" + school + ", pay=" + pay + "]";
System.out.println(person);
System.out.println(student);
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;
Devadharshini . P 717823L310
Page 13 of 24
Java Programming23CSR306
Devadharshini . P 717823L310
Page 14 of 24
Java Programming23CSR306
this.price = price;
this.qty = qty;
}
Devadharshini . P 717823L310
Page 15 of 24
Java Programming23CSR306
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";
int x = s1.compareTo(s2);
System.out.println("Comparison of s1 with s2: " + x);
Devadharshini . P 717823L310
Page 19 of 24
Java Programming23CSR306
String s3 = s1 + s2;
System.out.println("Combined string s3: " + s3);
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;
}
Devadharshini . P 717823L310
Page 22 of 24
Java Programming23CSR306
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