Master interfaces, file I/O operations, and advanced string manipulation.
Learn abstraction with interfaces, read/write files, and manipulate strings like a pro.
Drawable interface and implement it in Circle and Rectangle classes to demonstrate abstraction and polymorphism.void draw(); - prints shape being drawndouble getArea(); - returns calculated areadouble radiusMath.PI * radius * radiusdouble width, heightimplements keyword to use interfacepublic abstractDrawing Circle with radius 5.0
Area: 78.54
Drawing Rectangle 4.0 x 6.0
Area: 24.0
interface Drawable { void draw(); double getArea(); }class Circle implements Drawable { }Math.PI * radius * radiusDrawable shape = new Circle(5);Review feedback below
import java.io.*; (FileWriter, BufferedReader, FileReader, IOException)try (FileWriter writer = new FileWriter("data.txt")) { }writer.write()try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) { }reader.readLine()try-with-resources - automatically closes file (Java 7+)FileWriter - writes characters to fileBufferedReader - efficient reading with bufferIOException for file operationsWriting to file...
File written successfully!
Reading from file:
Line 1: Hello, Java!
Line 2: File I/O is easy.
Line 3: Keep learning!
import java.io.*;try (FileWriter w = new FileWriter("file.txt")) { w.write("text"); }try (BufferedReader r = new BufferedReader(new FileReader("file.txt"))) { }while ((line = reader.readLine()) != null) { }Review feedback below
length() - get string lengthtoUpperCase() / toLowerCase() - change casetrim() - remove leading/trailing whitespacecharAt(index) - get character at positionindexOf("text") - find position of substringsubstring(start, end) - extract portionequals() - compare strings (NOT ==)contains() - check if contains substringstartsWith() / endsWith()replace(old, new) - replace occurrencessplit(delimiter) - split into arrayOriginal: " 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!"]
str.trim()str.indexOf("search") returns -1 if not foundstr.substring(start, end) - end is exclusiveString[] arr = str.split(",");str1.equals(str2) NOT str1 == str2Review feedback below