Java Programming Labs

Master interfaces, file I/O operations, and advanced string manipulation.

Interfaces, File I/O & String Processing - Module 4

Learn abstraction with interfaces, read/write files, and manipulate strings like a pro.

Lab 10: Interfaces & Abstraction
Advanced
Coding Challenge
Your Task: Create a Drawable interface and implement it in Circle and Rectangle classes to demonstrate abstraction and polymorphism.

Detailed Requirements:
1. Create interface "Drawable" with two abstract methods:
   � void draw(); - prints shape being drawn
   � double getArea(); - returns calculated area

2. Create class "Circle" implementing Drawable:
   � Field: double radius
   � Constructor taking radius
   � Implement draw() to print "Drawing Circle"
   � Implement getArea() returning Math.PI * radius * radius

3. Create class "Rectangle" implementing Drawable:
   � Fields: double width, height
   � Implement both interface methods

4. Demonstrate polymorphism using Drawable array/references.

Interface Concepts:
� Interface = contract (what methods a class MUST have)
implements keyword to use interface
� All interface methods are implicitly public abstract
� A class can implement multiple interfaces

Expected Output:
Drawing Circle with radius 5.0 Area: 78.54 Drawing Rectangle 4.0 x 6.0 Area: 24.0

Requirements Checklist

Create Drawable interface with draw() and getArea()
Create Circle class implementing Drawable
Create Rectangle class implementing Drawable
Implement getArea() with correct calculations
Use Math.PI for circle area
Demonstrate polymorphism with Drawable reference
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Interface: interface Drawable { void draw(); double getArea(); }
� Implement: class Circle implements Drawable { }
� Circle area: Math.PI * radius * radius
� Polymorphism: Drawable shape = new Circle(5);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 11: File Input/Output
Advanced
Coding Challenge
Your Task: Write data to a file and read it back using Java's I/O classes with proper exception handling and resource management.

Detailed Requirements:
1. Import required classes:
   import java.io.*; (FileWriter, BufferedReader, FileReader, IOException)

2. Write to file using FileWriter:
   � Use try-with-resources: try (FileWriter writer = new FileWriter("data.txt")) { }
   � Write multiple lines using writer.write()

3. Read from file using BufferedReader:
   � try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) { }
   � Read line by line with reader.readLine()

4. Handle IOException with catch block.

Key Concepts:
try-with-resources - automatically closes file (Java 7+)
FileWriter - writes characters to file
BufferedReader - efficient reading with buffer
� Always handle IOException for file operations

Expected Output:
Writing to file... File written successfully! Reading from file: Line 1: Hello, Java! Line 2: File I/O is easy. Line 3: Keep learning!

Requirements Checklist

Import java.io classes
Use FileWriter to write to file
Use try-with-resources syntax
Use BufferedReader to read file
Read lines with readLine() in loop
Handle IOException with catch block
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Import: import java.io.*;
� Write: try (FileWriter w = new FileWriter("file.txt")) { w.write("text"); }
� Read: try (BufferedReader r = new BufferedReader(new FileReader("file.txt"))) { }
� Loop: while ((line = reader.readLine()) != null) { }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 12: String Manipulation
Intermediate
Coding Challenge
Your Task: Master essential String methods by processing and transforming text data using Java's built-in String methods.

Detailed Requirements:
1. Basic String operations:
   � length() - get string length
   � toUpperCase() / toLowerCase() - change case
   � trim() - remove leading/trailing whitespace

2. Searching and extracting:
   � charAt(index) - get character at position
   � indexOf("text") - find position of substring
   � substring(start, end) - extract portion

3. Comparison and checking:
   � equals() - compare strings (NOT ==)
   � contains() - check if contains substring
   � startsWith() / endsWith()

4. Transformation:
   � replace(old, new) - replace occurrences
   � split(delimiter) - split into array

Expected Output:
Original: " Hello, Java World! " Trimmed: "Hello, Java World!" Length: 18 Uppercase: "HELLO, JAVA WORLD!" Contains "Java": true Index of "World": 12 Substring(7,11): "Java" Replace: "Hello, Python World!" Split by comma: ["Hello", " Java World!"]

Requirements Checklist

Use length(), trim(), toUpperCase()
Use charAt() to get character
Use indexOf() to find substring
Use substring() to extract text
Use contains() or equals() for checking
Use replace() or split() for transformation
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Trim whitespace: str.trim()
� Find text: str.indexOf("search") returns -1 if not found
� Extract: str.substring(start, end) - end is exclusive
� Split: String[] arr = str.split(",");
� Compare: Use str1.equals(str2) NOT str1 == str2
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below