14-3-2024
Task 1 : Hierarchical inheritance-based Java code for a library management
// Parent class
class LibraryItem {
protected String title;
protected int year;
protected String author;
public LibraryItem(String title, int year, String author) {
// assign the value to class fields
}
public void displayInfo() {
// write code for display title,year, author
}
}
// Child class inheriting from LibraryItem
class Book extends LibraryItem {
private int pageCount;
public Book(String title, int year, String author, int pageCount) {
super(fill this field);
this.pageCount = pageCount;
}
public void displayInfo() {
super.displayInfo();
System.out.println("Page Count: " + pageCount);
}
}
// Child class inheriting from LibraryItem
class Journal extends LibraryItem {
private String publisher;
public Journal(String title, int year, String author, String publisher) {
super(title, year, author);
this.publisher = publisher;
}
public void displayInfo() {
super.displayInfo();
System.out.println("Publisher: " + publisher);
}
}
// Main class
public class LibraryManagementSystem {
public static void main(String[] args) {
Book book = new Book("Java Programming", 2020, "John Smith", 500);
Journal journal = new Journal("Science Journal", 2021, "Scientific Society", "Science Press");
System.out.println("Book Information:");
book.displayInfo();
System.out.println();
System.out.println("Journal Information:");
journal.displayInfo();
}
}
Task :: modify the code by adding 10 different books details and display it
Task 2 write content into the file using FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) throws IOException
//throws IOException {
String content = "Hello, world! This is a sample text to write to a file.";
// Specify the file path
String filePath = "output.txt";
// Write content to file
FileWriter writer = new FileWriter(filePath);
writer.write(content);
writer.close();
System.out.println("Data has been written to " + filePath);
}
}
Modify the code such a way that user can enter the text from the console window and display it.
Note : “throws IOException” this part I will explain in the class . for the time being type as it is. It
is basically used for handling error
Task 3 reading data from a file
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFromFileExample {
public static void main(String[] args) throws IOException {
// Specify the file path
String filePath = "output.txt";
// Read content from file
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
}
End of today’s lab task
----------------------------------------------------------------------------------------------------------------------------------
Warm-up questions
Explain the concept of method nesting in Java and analyze its role in enhancing code
structure and readability. Include examples to support your discussion.
Explain nesting of methods in java
class nesting{
int x,y;
nesting(int a, int b)
{
x=a;
y=b;
}
int large()
{
if(x>=y)
return (x);
else
return (y);
}
void display()
{
int lar=large();
System.out.println("result="+lar);
}
};
class myclass1
{
public static void main(String[] args) {
nesting n=new nesting(40,30);
n.display();
}
}
Precvious class code
Just for your understanding how we can access a private variable in another class
// Superclass with a private variable
class Superclass {
private int privateVariable;
public Superclass(int privateVariable) {
this.privateVariable = privateVariable;
}
public void display() {
System.out.println("Private variable in superclass: " + privateVariable);
}
// Getter method to access the private variable
public int getPrivateVariable() {
return privateVariable;
}
// Setter method to modify the private variable
public void setPrivateVariable(int privateVariable) {
this.privateVariable = privateVariable;
}
}
// Subclass extending the superclass
class Subclass extends Superclass {
public Subclass(int privateVariable) {
super(privateVariable);
}
public void displayFromSubclass() {
// Now accessing privateVariable through getter method
System.out.println("Private variable in subclass: " + getPrivateVariable());
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass(10);
obj.display(); // This will print "Private variable in superclass: 10"
obj.displayFromSubclass(); // This will print "Private variable in subclass: 10"
}
}
Class model for library management as part of your project , just to understand how many classes
and its hierarchy