UI24PC320CS · OOP THROUGH JAVA · UNIT I · LAB 1 · AFTER CLASS 8 EXACTLY
Class 8 was rehearsal.
This is the performance.
Your first syllabus-graded Java lab — and there is no rush in it. Everything you will type was taught, slowly and carefully, in Class 8: a class, its constructors, its methods, and overloading. This sheet walks the same path again at lab pace: warm up the theory, warm up the fingers, then three exercises that grow one Book.java gently, one idea at a time.
"A program to demonstrate the concept of class with constructors, methods and overloading."
STEP 0 · BEFORE ANY CODE — MAKE THE FOLDER, SO EVERY FILE HAS A HOME
C:\Users\diya> cd Desktop\java-practice
C:\Users\diya\Desktop\java-practice> mkdir lab-01
C:\Users\diya\Desktop\java-practice> cd lab-01
C:\Users\diya\Desktop\java-practice\lab-01> _
Same growing tree as always: java-practice already holds lab-00, class-02, class-03, class-04 and class-08 — today it grows one more branch. If your prompt doesn't end in lab-01, stop and cd until it does. ✦
The one rule of every lab in this course: type, never paste. Your fingers are learning a language, and they learn it at their own honest speed — that is fine, that is the point. Every solution sheet on this page stays locked until your own attempt exists as a .java file on your machine. Not ceremony; that is how the marks reach your hands in the real evaluation.
THE COMPLETE FILE PLAN · 5 PROGRAMS
Three rehearsals at home,
two performances in the lab.
Each finger drill mirrors one graded move on purpose — same shape, smaller stakes. Do the prelab honestly, without hurrying it, and lab day feels like déjà vu instead of panic.
| # | FILE (EXACT NAME) | WHEN | MECHANICS | REHEARSES FOR |
|---|---|---|---|---|
| 1 | Pen.java | PRELAB · HOME | two overloaded constructors | Exercise 1 |
| 2 | Greeter.java | PRELAB · HOME | two overloaded methods | Exercise 2 |
| 3 | PenArray.java | PRELAB · HOME | Pen[3] + for loop | Exercise 3's driver |
| 4 | Book.java | LAB DAY · EX 1+2 | 3 fields · 2 constructors · 2 describe() overloads | — |
| 5 | BookDriver.java | LAB DAY · EX 3 | Book[3] · both constructors · both overloads | — |
Every solution sheet renders as non-copyable text, revealed one line at a time. That is not meanness: the entire point is the unhurried path from your eyes through your fingers to the keyboard. Type every character, including the ones you think you'd never mistype. Muscle memory is the deliverable.
FIRST, SLOWLY · READ THE QUESTION LIKE AN EXAMINER
Four graded words. Know where each one is earned.
Before any typing, sit with the R-24 sentence for a minute. It names exactly four ideas — class · constructors · methods · overloading — and an examiner checks each word against your submission. This grid is that check, done calmly and in advance, with the Class 8 part that taught each word beside it.
| SYLLABUS WORD | WHERE YOU EARN IT IN THIS LAB | TAUGHT IN | EX 1 | EX 2 | EX 3 |
|---|---|---|---|---|---|
| class | Book.java — 3 fields plus everything below them, living in one named unit | C8 · Part 3 | ✔ | ✔ | ✔ |
| constructors | Two birth certificates: all-three-fields, and title+author with price defaulting to 0.0 | C8 · Parts 9–10 | ✔ | ✔ | |
| methods | describe() — operations on the object's own facts | C8 · Parts 3, 5 | ✔ | ✔ | |
| overloading | TWICE, unhurried: overloaded constructors in Ex 1, then overloaded describe() in Ex 2 — the same fingerprint rule, met twice so it settles in | C8 · Parts 6–7, 10 | ✔ | ✔ | ✔ |
Notice the design: Exercise 3 exercises every word at once — that is why it comes last, after the other two have made each word familiar. If your Ex 3 runs clean, you have demonstrated the full syllabus line in one program. The examiner's job becomes ticking boxes; your job, done gently across three exercises, is making every box tickable.
PRELAB THEORY · AT HOME · RULED NOTEBOOK OUT · NO HURRY
Five questions before you touch the keyboard.
Pen first, always. Lab evaluations open with viva questions — these five, or their cousins. The first three are guided: each carries a hint pointing back at the exact Class 8 moment that taught it. The last two are independent — no hint, because the viva won't bring one either. Take your time with each; the model answers wait, locked, on the next page.
Define a constructor. Then name the TWO things that make it different from an ordinary method.
void?YOUR RULED NOTEBOOK — DEFINITION + BOTH DIFFERENCES
What happens to the free default constructor the moment you write your own? Why does new Book() suddenly stop compiling?
YOUR RULED NOTEBOOK — WHAT HAPPENS + WHY IT STOPS COMPILING
What exactly is a method's signature — and name two things that are NOT part of it.
YOUR RULED NOTEBOOK — THE SIGNATURE + TWO NON-MEMBERS
Can two methods in one class differ only in return type? Answer first, then give the reason. No hint on this one.
YOUR RULED NOTEBOOK — YES OR NO, THEN THE WHY
A Book field price (a double) is never assigned. What does it hold — and how is that different from an unassigned LOCAL double inside main? No hint on this one either.
YOUR RULED NOTEBOOK — THE FIELD'S VALUE + THE LOCAL'S FATE
Done all five in pen? Good — and if any of them made you pause, that pause was the useful part. The model answers come next, locked. Open them only after all five carry ink; compare gently, and treat every difference as a finding, not a failure.
SOLUTION SHEET · ANSWER + REASONING + THE COMMON SLIP
Model answers — earn them first.
Separate page from the questions, on purpose — no sideways glancing. Each card gives the correct answer, the reasoning behind it, and the slip students most often make, so the viva holds no surprises.
Five written answers in the notebook first — the viva has no unlock button.
Answer. A constructor is a special member invoked automatically by new to initialise the freshly made object. Its two specialties: its name IS the class's name, exactly — and it declares no return type, not even void.
Reasoning. The constructor's job is birth, not computation — there is nothing to return; the new expression itself hands back the reference.
Common slip. Writing void Book(...) "to be safe" — that quietly makes an ordinary method named Book that never runs at birth. The viva loves this one.
Keep this. Same name, no return type — both, always, or it isn't a constructor.
Answer. It is withdrawn. Java gifts a no-argument default constructor ONLY while a class defines no constructor at all; the moment you write any constructor of your own, the gift disappears.
Reasoning. Once you have declared how Books are born, Java stops guessing on your behalf. new Book() then compiles only if YOU wrote a no-arg constructor yourself.
Common slip. Assuming the free one and yours coexist. They don't — the real message is error: constructor Book in class Book cannot be applied to given types.
Keep this. Write one constructor and you own ALL of them.
Answer. Signature = method name + parameter list — the types, their order, their count. NOT part of it: the return type and the parameter names.
Reasoning. The signature is what the compiler compares at a call site to choose between same-named methods; parameter names and return types don't help it choose, so they don't count.
Common slip. Believing greet(String name) and greet(String person) are different methods. Same fingerprint — the compiler rejects the second as a duplicate.
Keep this. Name + parameter types — nothing else is fingerprint.
Answer. No. Both would carry the same signature, and at a call site the compiler may have no return type to choose by — a caller is free to ignore the result entirely.
Reasoning. In b.size(); written as a bare statement, which of the two would you mean? Java refuses to guess: error: method size() is already defined.
Keep this. Return type never distinguishes overloads — only the parameter list does.
Answer. The field holds 0.0 — fields receive automatic defaults (0, 0.0, false, null). An unassigned local double is a compile error the moment you read it: locals get no defaults at all.
Reasoning. Objects are built in one clean sweep on the heap, so Java zeroes their fields; a local lives on the stack and Java insists you show it a value first — error: variable x might not have been initialized.
Common slip. Fearing that constructor 2's untouched price is "garbage". It isn't — it is a guaranteed, dependable 0.0. Exercise 1 leans on exactly this guarantee.
Keep this. Fields forgive; locals don't.
PRELAB CODING · AT HOME · 15–20 MINUTES · SMALL ON PURPOSE
Three finger drills. Tiny programs, real muscles.
Each drill is a mini problem statement — problem, requirements, a sample run to match, and a plan line for your notebook. No code on this page, and no hurry either: each drill is deliberately small enough to finish in five unhurried minutes. Together they rehearse every muscle lab day will ask for.
- Class named exactly
Pen, saved asPen.javainlab-01. - Two fields:
String inkanddouble price. - Constructor 1 takes both values; constructor 2 takes only the ink and sets price to
5.0itself. - A
mainthat builds one pen through each constructor and prints both asink + " Rs. " + price.
blue Rs. 12.0 then black Rs. 5.0 — the 5.0 must come from constructor 2, not from main.new two println. Same name twice, different parameter lists — that is the whole trick, met calmly.- Class
Greeter, saved asGreeter.java. greet(String name)prints one line:Hello,+ name +!greet(String name, int times)prints that same linetimestimes, using a for loop.- A
mainthat calls both — once each — on one Greeter object.
greet("Diya") Hello, Diya! once · greet("Arjun", 2) Hello, Arjun! twice, on two lines.- Class
PenArray, saved asPenArray.java— in the same folder asPen.java, so it can see your Pen class. - Declare
Pen[] box = new Pen[3]; - Fill all three slots — use constructor 1 for two of them and constructor 2 for one.
- One for loop prints every pen as
ink + " Rs. " + price.
Rs. 5.0.new Pen[3] makes three empty slots, not three pens. Skip a fill line and the loop greets you with java.lang.NullPointerException — better to meet it calmly at your own desk than on lab day's clock.Write in your notebook exactly where it stopped making sense — the line, the error, the confusion — and bring that note to the lab. A precise question is worth more than a forced solution. The solution sheet on the next page unlocks after your honest attempt exists as a file, however unfinished.
SOLUTION SHEET · ONLY AFTER YOUR THREE FILES EXIST
Pen.java, one line at a time — then check the other two against the specs.
Drill 1 is solved here in full, revealed step by step so you can compare your structure against it move by move. Drills 2 and 3 get precise checklists instead of code — by the time you finish Pen.java honestly, you will not need their code, and lab day will prove it.
Three .java files on your Desktop first — the unlock button checks your conscience, not your disk.
C:\Users\diya\Desktop\java-practice\lab-01> javac Pen.java
C:\Users\diya\Desktop\java-practice\lab-01> java Pen
blue Rs. 12.0
black Rs. 5.0
Line 18 handed Pen only an ink — and the second output line still shows a dependable 5.0, because constructor 2 filled it at birth. Two birth certificates, one class. ✦
greet — fingerprints (String) and (String, int). The two-parameter one loops times times; nicest version calls greet(name) inside the loop. If yours compiles and greets Arjun twice, it is right.
new Pen[3], then three fill lines, then one for loop up to box.length. If any run ever showed NullPointerException, find the slot you forgot to fill — that experience is the drill's real gift.
Desktop\java-practice\lab-01\, and you can say aloud, without notes, why Pen(String) and Pen(String, double) may share a name. That sentence is your ticket in.
LAB DAY · THE RHYTHM OF THE TWO HOURS
Brief, type, run, unlock — the same four beats, three times over.
Each exercise on this page follows one calm, repeating rhythm. Read the brief and predict on paper. Type your attempt. Compile and run — javac first, then java, every single time. Only then unlock the solution sheet and compare. Three exercises, same four beats — by Exercise 3 the rhythm will feel like yours.
Slowly. Underline the marks map. Write your prediction of the output in the notebook before touching the keyboard.
In Notepad++, saved with the exact file name in the exact folder. Type, never paste — same rule as every lab.
javac Book.java first — only when it comes back silent do you type java .... Errors are information, not verdicts.
Open the solution sheet, step it line by line, and compare structure against yours. Differences of taste are fine; differences of structure go in the notebook.
Exercise 3's driver builds Books, so BookDriver.java must sit in the same lab-01 folder as Book.java — that is how javac finds the Book class without any imports. Split them into different folders and the compiler politely reports error: cannot find symbol.
WHAT YOUR FOLDER LOOKS LIKE WHEN YOU LEAVE THE LAB
java-practice already holds lab-00, class-02, class-03, class-04 and class-08. Today's five programs make lab-01 its newest branch — one Desktop root, one folder per session, every file findable months from now when revision season arrives.
EXERCISE 1 · GRADED · 30 MARKS · CLASS + OVERLOADED CONSTRUCTORS
A Book, born two ways.
The first graded program is Drill 1 wearing exam clothes — a class with three fields and two birth certificates. You have already made this move once at home with Pen. Same shape, new domain, real marks.
PROBLEM The college library catalogues books. Every book has a title, an author and a price — but donated books arrive with no price on record, and the catalogue must still accept them gracefully.
BUILD Book.java in Desktop\java-practice\lab-01\ containing:
- Three fields:
String title·String author·double price(6 marks) - Constructor 1: takes all three values and stores them with
this(12 marks) - Constructor 2: takes only title and author — a donated book — and settles price at
0.0(12 marks) - A small
mainthat builds one Book each way and prints both — your proof it works. Exercise 3 replaces this main with a real driver.
PREDICT Before typing: constructor 2 never mentions price. In your notebook, write what the donated book will print as its price — and why you are sure. Theory Q5 already gave you the answer.
YOUR RULED NOTEBOOK — PREDICTION + THE REASON
SOLUTION SHEET · EXERCISE 1 · AFTER YOUR ATTEMPT COMPILES OR STALLS HONESTLY
Book.java — watch the second constructor lean on the first.
One detail here is worth the whole page: constructor 2 does not copy constructor 1's work — it calls it, with this(title, author, 0.0). One line, no duplication, and any future change to how Books are born happens in exactly one place.
Your own Book.java first — even a half-finished one earns the unlock.
C:\Users\diya\Desktop\java-practice\lab-01> javac Book.java
C:\Users\diya\Desktop\java-practice\lab-01> java Book
Wings of Fire Rs. 399.0
Godaan Rs. 0.0
There is your prediction, confirmed on screen: the donated Godaan prints Rs. 0.0 — set deliberately by constructor 2, through constructor 1. ✦
this — 12. Constructor 2 with the two-argument fingerprint settling price at 0.0 — 12. The examiner reads your constructor headers first; make lines 6 and 12 immaculate.
this(title, author, 0.0) means "run my own three-argument constructor with these values". It must be the first statement inside constructor 2 — Java insists. If you stored the fields by hand instead, you still earn the marks; the chained version is simply the habit worth keeping.
EXERCISE 2 · GRADED · 30 MARKS · OVERLOADED METHODS · SAME FILE, GROWN
Teach the Book to describe itself — in two voices.
No new file. Exercise 2 grows inside Book.java: two methods, both named describe, with different fingerprints. This is Drill 2's Greeter move, transplanted into a graded class. The overloading rule is already in your fingers; today it simply earns marks.
PROBLEM The catalogue screen sometimes needs a short listing — title and author — and sometimes the full card with the price. Same book, two voices, chosen by how the method is called.
BUILD Add to your existing Book.java:
void describe()— prints one line:title + " by " + author(12 marks)void describe(boolean withPrice)— when true, prints the full card including" Rs. " + price; when false, gives the short listing (12 marks)- Update
mainto call both voices on both books (6 marks)
DESIGN QUESTION Before typing, answer in the notebook: when withPrice is false, should the boolean version re-write the short line — or call describe()? One of these survives a future format change untouched. Choose, and write why.
YOUR RULED NOTEBOOK — YOUR CHOICE + THE WHY
SOLUTION SHEET · EXERCISE 2 · THE TWO VOICES, STEPPED
Two fingerprints, one delegation — the design that survives change.
Only the new members are stepped here — they slot into your Book.java below the constructors. Watch line 5: the boolean voice does not repeat the short line; it delegates to describe(), exactly the instinct Drill 2 rewarded.
Your design answer in the notebook first — the code will confirm or gently correct it.
C:\Users\diya\Desktop\java-practice\lab-01> javac Book.java
C:\Users\diya\Desktop\java-practice\lab-01> java Book
Wings of Fire by Kalam
Wings of Fire by Kalam Rs. 399.0
Every edit needs its javac before its java — the run above only shows the new voices because the recompile came first. Edit, save, javac, java. ✦
describe() printing the short line from the object's own fields — 12. describe(boolean) with the correct fingerprint and both branches — 12. main exercising both voices on both books — 6.
describe(), a future change to the short format — say, adding quotation marks around the title — is one edit in one place, and both voices update together. Re-writing the line in both methods means two edits and, one busy afternoon, a mismatch.
describe() and describe(boolean) differ in parameter list — the fingerprint. Theory Q4's trap sits right beside this: differing in return type alone would not compile.
EXERCISE 3 · GRADED · 25 MARKS · EVERYTHING AT ONCE, CALMLY
Three books on one shelf — the whole syllabus line in one program.
The finale is Drill 3's PenArray move with your finished Book class: an array of three, both constructors, both describe voices, one loop. Nothing here is new — that is precisely the design. This program is where the examiner ticks every box at once.
PROBLEM The library wants a shelf report: three catalogued books — one of them donated — each announced twice, short listing then full card.
BUILD BookDriver.java — a separate file in the same lab-01 folder:
- Declare and fill
Book[] shelf = new Book[3];(10 marks) - Use both constructors while filling — the donated book goes through the two-argument one (10 marks)
- One for loop: for each book call
describe()thendescribe(true)(5 marks)
PREDICT Six describe calls, two per book. In the notebook, write all six output lines in order, exactly — including what the donated book's full card says for its price. Then type, run, and compare line by line.
YOUR RULED NOTEBOOK — ALL SIX LINES, PREDICTED BEFORE THE RUN
SOLUTION SHEET · EXERCISE 3 · DRIVER + FULL RUN, STEPPED
BookDriver.java — and the six lines your notebook promised.
Step the code, then step the run, and hold your prediction beside the screen. Every line that matches is a concept you own; any line that differs points at exactly one thing to re-read. That comparison — not the typing — is where this exercise teaches.
Six predicted lines in the notebook first — the run is the reward.
C:\Users\diya\Desktop\java-practice\lab-01> javac BookDriver.java
C:\Users\diya\Desktop\java-practice\lab-01> java BookDriver
Wings of Fire by Kalam
Wings of Fire by Kalam Rs. 399.0
Godaan by Premchand
Godaan by Premchand Rs. 0.0
Malgudi Days by Narayan
Malgudi Days by Narayan Rs. 250.0
Six lines, two per book, shelf order 0, 1, 2 — and only Godaan, the donated one, carries Rs. 0.0. If your prediction matched all six, the syllabus line is yours. ✦
javac BookDriver.java compiles Book too, automatically, because the driver mentions it — both .class files appear together. Run the driver: java BookDriver, never java Book, for this exercise.
BEFORE YOU RAISE YOUR HAND · THE SIX CLASSIC SLIPS
Every year, the same six. Not you, not this year.
Each row is a slip your seniors actually made, the exact symptom it produces on screen, and the calm fix. Read them once now — then, on lab day, any error message you meet will already be an old acquaintance.
| # | THE SLIP | WHAT THE SCREEN SAYS | THE CALM FIX |
|---|---|---|---|
| 1 | Constructor writes title = title; without this | Compiles fine — then every field prints null | The parameter shadowed the field, and assigned to itself. this.title = title; — left side field, right side parameter. |
| 2 | Writing void Book(...) "to be safe" | error: constructor Book in class Book cannot be applied to given types at every new | Adding void made it an ordinary method, not a constructor. Delete the void — constructors declare no return type at all. |
| 3 | Only one describe voice written, called with the other fingerprint | error: method describe in class Book cannot be applied to given types | Check both fingerprints exist: () and (boolean). The call chooses by its argument list. |
| 4 | A shelf slot never filled before the loop | Compiles, runs, then Exception in thread "main" java.lang.NullPointerException | new Book[3] makes empty slots, not Books. Count your fill lines: three slots, three new. |
| 5 | Running before recompiling after an edit | The run stubbornly shows the old output | Nothing is wrong with your edit — you re-ran stale bytecode. Edit, save, javac, then java. Every time, in that order. |
| 6 | Opening brace on the same line as the header | Runs fine — reads unlike everything this course writes | House style is the brace on its own line, always. The examiner reads dozens of submissions; consistency reads as care. |
THE RUBRIC · 100 MARKS · READ IT BEFORE, NOT AFTER
Where every mark lives.
| COMPONENT | WHAT THE EXAMINER CHECKS | MARKS |
|---|---|---|
| Exercise 1 · Book class + constructors | Three fields · both constructor fingerprints · this used correctly · donated price settles at 0.0 | 30 |
| Exercise 2 · describe overloads | Both voices present · correct fingerprints · both branches behave · main exercises them | 30 |
| Exercise 3 · BookDriver | Array of 3 filled · both constructors used · loop calls both voices per book | 25 |
| Working practice | Correct file names · one lab-01 folder · javac before java without prompting | 5 |
| Viva | Two questions from the theory five, answered aloud without notes | 10 |
| TOTAL | 100 |
SUBMIT Before leaving the lab, show the evaluator: your lab-01 folder with all five .java files and their .class companions, one live run of java BookDriver on your machine, and your notebook open to the six predicted lines. That is the whole ceremony — no uploads, no zip files, just working programs in a tidy folder.