Unit 1 home
LAB 1 · SYLLABUS LAB CLASS + CTORS + OVERLOADING UNIT I · UI24PC320CS
LAB 1 · P 1/14PGDN NEXT POINT · PGUP BACK

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.

R-24 SYLLABUS · LAB 1 · WORD FOR WORD

"A program to demonstrate the concept of class with constructors, methods and overloading."

C7 · EXECUTION ENGINE C8 · YOUR FIRST REAL CLASS LAB 1 · YOU BUILD IT C9 · MEMORY & static
TIME BUDGET2 hours in the lab · 25–30 min prelab at home
PROGRAMS5 total — 3 finger drills at home + Book.java + BookDriver.java
MARKS100 — rubric on the last page, read it first
PREREQUISITEClass 8 — every mechanic already taught, nothing new here

STEP 0 · BEFORE ANY CODE — MAKE THE FOLDER, SO EVERY FILE HAS A HOME

EVERY FILE IN THIS LAB IS SAVED HERE — NO EXCEPTIONS C:\Users\<you>\Desktop\java-practice\lab-01\
COMMAND PROMPT — MAKE THE FOLDER FIRST, BEFORE ANY CODE

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)WHENMECHANICSREHEARSES FOR
1Pen.javaPRELAB · HOMEtwo overloaded constructorsExercise 1
2Greeter.javaPRELAB · HOMEtwo overloaded methodsExercise 2
3PenArray.javaPRELAB · HOMEPen[3] + for loopExercise 3's driver
4Book.javaLAB DAY · EX 1+23 fields · 2 constructors · 2 describe() overloads
5BookDriver.javaLAB DAY · EX 3Book[3] · both constructors · both overloads
The code on these lab pages cannot be selected or copied — by design. (Same rule as Lab 0.)

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 WORDWHERE YOU EARN IT IN THIS LABTAUGHT INEX 1EX 2EX 3
classBook.java — 3 fields plus everything below them, living in one named unitC8 · Part 3
constructorsTwo birth certificates: all-three-fields, and title+author with price defaulting to 0.0C8 · Parts 9–10
methodsdescribe() — operations on the object's own factsC8 · Parts 3, 5
overloadingTWICE, unhurried: overloaded constructors in Ex 1, then overloaded describe() in Ex 2 — the same fingerprint rule, met twice so it settles inC8 · 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.

1DEFINE + TWO DIFFERENCESGUIDED

Define a constructor. Then name the TWO things that make it different from an ordinary method.

HINTClass 8's birth-certificate picture: what is the constructor's name always equal to — and what does it declare where a method would declare void?

YOUR RULED NOTEBOOK — DEFINITION + BOTH DIFFERENCES

2EXPLAIN THE WITHDRAWALGUIDED

What happens to the free default constructor the moment you write your own? Why does new Book() suddenly stop compiling?

HINTClass 8 called it a gift with a condition. Under exactly what condition does Java give a class the no-argument constructor for free?

YOUR RULED NOTEBOOK — WHAT HAPPENS + WHY IT STOPS COMPILING

3NAME THE PARTSGUIDED

What exactly is a method's signature — and name two things that are NOT part of it.

HINTClass 8 called it the fingerprint. The fingerprint is what the compiler compares when two methods share a name — which two pieces of a declaration does it ignore?

YOUR RULED NOTEBOOK — THE SIGNATURE + TWO NON-MEMBERS

4YES/NO + REASONINDEPENDENT

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

5FIELD vs LOCALINDEPENDENT

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

ITEM MIX · 1 × DEFINE2 × EXPLAIN-WHY1 × YES/NO + REASON1 × COMPARE

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.

MODEL ANSWERS · PRELAB THEORY · ONE ANSWER PER PRESS
1DEFINE + TWO DIFFERENCES

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.

2EXPLAIN THE WITHDRAWAL

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.

3NAME THE PARTS

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.

4YES/NO + REASON

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.

5FIELD vs LOCAL

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.

ALL THREE FILES GO HERE — EXACT NAMES FROM THE FILE PLAN C:\Users\<you>\Desktop\java-practice\lab-01\ holds: Pen.java · Greeter.java · PenArray.java
DRILL 1 · PRELABPen.javaRehearses Exercise 1 — two ways for an object to be born
PROBLEM
A pen has an ink and a price. Some pens arrive with a known price; others arrive with only their ink named, and take the standard price. Give the class both birth certificates.
REQUIRE­MENTS
  • Class named exactly Pen, saved as Pen.java in lab-01.
  • Two fields: String ink and double price.
  • Constructor 1 takes both values; constructor 2 takes only the ink and sets price to 5.0 itself.
  • A main that builds one pen through each constructor and prints both as ink + " Rs. " + price.
SAMPLE RUN
no typing needed program prints blue Rs. 12.0 then black Rs. 5.0 — the 5.0 must come from constructor 2, not from main.
PLAN
fields constructor(both) constructor(ink only) main two new two println. Same name twice, different parameter lists — that is the whole trick, met calmly.
DRILL 2 · PRELABGreeter.javaRehearses Exercise 2 — one name, two method fingerprints
PROBLEM
A greeter that can say hello once — or, when asked, several times over. Same verb, two shapes of request: that is method overloading in its friendliest form.
REQUIRE­MENTS
  • Class Greeter, saved as Greeter.java.
  • greet(String name) prints one line: Hello, + name + !
  • greet(String name, int times) prints that same line times times, using a for loop.
  • A main that calls both — once each — on one Greeter object.
SAMPLE RUN
greet("Diya") Hello, Diya! once · greet("Arjun", 2) Hello, Arjun! twice, on two lines.
PLAN
class greet(one parameter) greet(two parameters) main. Bonus mark of taste: let the two-parameter version call the one-parameter version inside its loop — Exercise 2 rewards exactly this instinct.
DRILL 3 · PRELABPenArray.javaRehearses Exercise 3 — a shelf of three objects, walked with a loop
PROBLEM
Three pens live in one array. Build the shelf, fill every slot, then walk it with a for loop and print each pen. Exercise 3 is this exact shape with Books.
REQUIRE­MENTS
  • Class PenArray, saved as PenArray.java — in the same folder as Pen.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.
SAMPLE RUN
three lines, one per slot, in index order 0 1 2 — the constructor-2 pen must show Rs. 5.0.
PLAN
array of 3 fill slot 0, 1, 2 for loop println each. The trap to feel once at home: 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.
Stuck for more than 15 minutes on any drill? Stop — gently, and without guilt.

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.

SOLUTION · Pen.java · SAVED IN Desktop\java-practice\lab-01\
Pen.java — Notepad++
1public class Pen
2{
3 String ink;
4 double price;
5 Pen(String ink, double price)
6 {
7 this.ink = ink;
8 this.price = price;
9 }
10 Pen(String ink)
11 {
12 this.ink = ink;
13 this.price = 5.0;
14 }
15 public static void main(String[] args)
16 {
17 Pen a = new Pen("blue", 12.0);
18 Pen b = new Pen("black");
19 System.out.println(a.ink + " Rs. " + a.price);
20 System.out.println(b.ink + " Rs. " + b.price);
21 }
22}
COMMAND PROMPT — javac FIRST, java SECOND, ALWAYS

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. ✦

Check your Greeter.java against this: two methods, both named 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.
Check your PenArray.java against this: 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.
Checkpoint before lab day: all three programs compile and run from 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.

1 · READ THE BRIEF

Slowly. Underline the marks map. Write your prediction of the output in the notebook before touching the keyboard.

2 · TYPE YOUR ATTEMPT

In Notepad++, saved with the exact file name in the exact folder. Type, never paste — same rule as every lab.

3 · COMPILE, THEN RUN

javac Book.java first — only when it comes back silent do you type java .... Errors are information, not verdicts.

4 · UNLOCK & COMPARE

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.

One folder holds everything — Book.java and BookDriver.java must live together.

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

Desktop\java-practice\lab-01\ — END OF LAB DAY
Desktop\java-practice\lab-01\
Pen.java + .class <- drill 1
Greeter.java + .class <- drill 2
PenArray.java + .class <- drill 3
Book.java + .class <- exercises 1 + 2 · graded
BookDriver.java + .class <- exercise 3 · graded
The tree keeps growing, gently. 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 main that 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.

SOLUTION · EXERCISE 1 · Book.java · 30 MARKS
Book.java — Notepad++
1public class Book
2{
3 String title;
4 String author;
5 double price;
6 Book(String title, String author, double price)
7 {
8 this.title = title;
9 this.author = author;
10 this.price = price;
11 }
12 Book(String title, String author)
13 {
14 this(title, author, 0.0);
15 }
16 public static void main(String[] args)
17 {
18 Book p = new Book("Wings of Fire", "Kalam", 399.0);
19 Book d = new Book("Godaan", "Premchand");
20 System.out.println(p.title + " Rs. " + p.price);
21 System.out.println(d.title + " Rs. " + d.price);
22 }
23}
COMMAND PROMPT — javac FIRST, java SECOND, ALWAYS

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. ✦

Marks map · 30. Fields declared with correct types — 6. Constructor 1 storing all three with this12. 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.
Line 14, slowly. 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 main to 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.

SOLUTION · EXERCISE 2 · THE NEW MEMBERS OF Book.java · 30 MARKS
Book.java — the two describe voices (inside the class)
1 void describe()
2 {
3 System.out.println(title + " by " + author);
4 }
5 void describe(boolean withPrice)
6 {
7 if (withPrice)
8 {
9 System.out.println(title + " by " + author + " Rs. " + price);
10 }
11 else
12 {
13 describe();
14 }
15 }
COMMAND PROMPT — RECOMPILE, THEN RUN AGAIN

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. ✦

Marks map · 30. 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.
Line 13 is the design answer. Because the false-branch calls 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.
Notice what makes this legal: 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() then describe(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.

SOLUTION · EXERCISE 3 · BookDriver.java · 25 MARKS
BookDriver.java — Notepad++
1public class BookDriver
2{
3 public static void main(String[] args)
4 {
5 Book[] shelf = new Book[3];
6 shelf[0] = new Book("Wings of Fire", "Kalam", 399.0);
7 shelf[1] = new Book("Godaan", "Premchand");
8 shelf[2] = new Book("Malgudi Days", "Narayan", 250.0);
9 for (int i = 0; i < shelf.length; i++)
10 {
11 shelf[i].describe();
12 shelf[i].describe(true);
13 }
14 }
15}
COMMAND PROMPT — javac FIRST, java SECOND, ALWAYS

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. ✦

Marks map · 25. Array declared and all three slots filled — 10. Both constructors used, donated book through the two-argument one — 10. Loop calling both voices per book — 5.
Compile order that always works: 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 SLIPWHAT THE SCREEN SAYSTHE CALM FIX
1Constructor writes title = title; without thisCompiles fine — then every field prints nullThe parameter shadowed the field, and assigned to itself. this.title = title; — left side field, right side parameter.
2Writing void Book(...) "to be safe"error: constructor Book in class Book cannot be applied to given types at every newAdding void made it an ordinary method, not a constructor. Delete the void — constructors declare no return type at all.
3Only one describe voice written, called with the other fingerprinterror: method describe in class Book cannot be applied to given typesCheck both fingerprints exist: () and (boolean). The call chooses by its argument list.
4A shelf slot never filled before the loopCompiles, runs, then Exception in thread "main" java.lang.NullPointerExceptionnew Book[3] makes empty slots, not Books. Count your fill lines: three slots, three new.
5Running before recompiling after an editThe run stubbornly shows the old outputNothing is wrong with your edit — you re-ran stale bytecode. Edit, save, javac, then java. Every time, in that order.
6Opening brace on the same line as the headerRuns fine — reads unlike everything this course writesHouse 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.

COMPONENTWHAT THE EXAMINER CHECKSMARKS
Exercise 1 · Book class + constructorsThree fields · both constructor fingerprints · this used correctly · donated price settles at 0.030
Exercise 2 · describe overloadsBoth voices present · correct fingerprints · both branches behave · main exercises them30
Exercise 3 · BookDriverArray of 3 filled · both constructors used · loop calls both voices per book25
Working practiceCorrect file names · one lab-01 folder · javac before java without prompting5
VivaTwo questions from the theory five, answered aloud without notes10
TOTAL100

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.