From fc4d96a9e3e20f8ddf035c16fc32b1effb75481b Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sun, 28 Jan 2024 06:38:55 +0000 Subject: Complete all Java assignments --- java/buildall | 3 +- java/code/AbstractShapeCalculations.java | 82 +++++++++++++-------------- java/code/CircularBaseExample.java | 2 +- java/code/EmployeeDisplay.java | 89 +++++++++++++++--------------- java/code/ThreadsExample.java | 4 +- java/contents.typ | 49 ++++++++++++++++ java/cover.tpl.typ | 37 +++++++++++++ java/output/AbstractShapeCalculations.typ | 19 +++++++ java/output/CircularBaseExample.typ | 17 ++++++ java/output/CustomExceptionExample.typ | 13 +++++ java/output/InnerClassExample.typ | 8 +++ java/output/InterfaceExample.typ | 11 ++++ java/output/PackageExample.typ | 8 +++ java/output/PackageInterfaceExample.typ | 8 +++ java/output/RuntimePolymorphismExample.typ | 9 +++ java/output/Template.typ | 4 ++ java/output/ThreadsExample.typ | 73 ++++++++++++++++++++++++ java/prelude.typ | 43 +-------------- java/state.sql | 22 ++++---- java/template.typ | 24 +++++--- java/text/AbstractShapeCalculations.typ | 36 ++++++++++++ java/text/CircularBaseExample.typ | 37 +++++++++++++ java/text/CuboidCalculations.typ | 26 +++++++++ java/text/CustomExceptionExample.typ | 23 ++++++++ java/text/EmployeeDisplay.typ | 31 +++++++++++ java/text/InnerClassExample.typ | 28 ++++++++++ java/text/InterfaceExample.typ | 39 +++++++++++++ java/text/PackageExample.typ | 28 ++++++++++ java/text/PackageInterfaceExample.typ | 32 +++++++++++ java/text/RuntimePolymorphismExample.typ | 31 +++++++++++ java/text/ThreadsExample.typ | 36 ++++++++++++ 31 files changed, 721 insertions(+), 151 deletions(-) create mode 100644 java/contents.typ create mode 100644 java/cover.tpl.typ create mode 100644 java/output/AbstractShapeCalculations.typ create mode 100644 java/output/CircularBaseExample.typ create mode 100644 java/output/CustomExceptionExample.typ create mode 100644 java/output/InnerClassExample.typ create mode 100644 java/output/InterfaceExample.typ create mode 100644 java/output/PackageExample.typ create mode 100644 java/output/PackageInterfaceExample.typ create mode 100644 java/output/RuntimePolymorphismExample.typ create mode 100644 java/output/Template.typ create mode 100644 java/output/ThreadsExample.typ create mode 100644 java/text/AbstractShapeCalculations.typ create mode 100644 java/text/CircularBaseExample.typ create mode 100644 java/text/CuboidCalculations.typ create mode 100644 java/text/CustomExceptionExample.typ create mode 100644 java/text/EmployeeDisplay.typ create mode 100644 java/text/InnerClassExample.typ create mode 100644 java/text/InterfaceExample.typ create mode 100644 java/text/PackageExample.typ create mode 100644 java/text/PackageInterfaceExample.typ create mode 100644 java/text/RuntimePolymorphismExample.typ create mode 100644 java/text/ThreadsExample.typ (limited to 'java') diff --git a/java/buildall b/java/buildall index 4468d35..6777b7b 100644 --- a/java/buildall +++ b/java/buildall @@ -1,4 +1,5 @@ #!/bin/sh cat prelude.typ > all.typ -awk '$2 == "#" { printf("#include(\"text/%s.typ\")\n", $1) }' alist.txt >> all.typ +sh havebeendone | awk '{ printf("#include(\"text/%s.typ\")\n", $2) }' >> all.typ typst compile --root $PWD all.typ docs/all.pdf +pdftk docs/all.pdf cat 1 4-7 10-end output docs/java-all.pdf diff --git a/java/code/AbstractShapeCalculations.java b/java/code/AbstractShapeCalculations.java index 3f18c0c..2b13279 100644 --- a/java/code/AbstractShapeCalculations.java +++ b/java/code/AbstractShapeCalculations.java @@ -2,54 +2,54 @@ import java.util.Scanner; import java.util.InputMismatchException; abstract class Shape { - abstract double area(); - abstract void display(); + abstract double area(); + abstract void display(); } class Rectangle extends Shape { - double length, width; - Rectangle(double len, double wid) { - length = len; - width = wid; - } - double area() { - return length * width; - } - void display() { - System.out.println("A rectangle with length " + length + " units and width " + width + " units has the area of " + area() + " square units."); - } + double length, width; + Rectangle(double len, double wid) { + length = len; + width = wid; + } + double area() { + return length * width; + } + void display() { + System.out.println("A rectangle with length " + length + " units and width " + width + " units has the area of " + area() + " square units."); + } } class Circle extends Shape { - double radius; - Circle(double r) { - radius = r; - } - double area() { - return Math.PI * radius * radius; - } - void display() { - System.out.println("A circle with radius " + radius + " units has the area of " + area() + " square units."); - } + double radius; + Circle(double r) { + radius = r; + } + double area() { + return Math.PI * radius * radius; + } + void display() { + System.out.println("A circle with radius " + radius + " units has the area of " + area() + " square units."); + } } class Triangle extends Shape { - double a, b, c; - Triangle(double s1, double s2, double s3) { - a = s1; - b = s2; - c = s3; - } - double perimeter() { - return a + b + c; - } - double area() { - var s = perimeter() / 2.0; - return Math.sqrt(s * (s - a) * (s - b) * (s - c)); - } - void display() { - System.out.println("A triangle with side lengths " + a + " units, " + b + "units and " + c + " units has the area of " + area() + " square units."); - } + double a, b, c; + Triangle(double s1, double s2, double s3) { + a = s1; + b = s2; + c = s3; + } + double perimeter() { + return a + b + c; + } + double area() { + var s = perimeter() / 2.0; + return Math.sqrt(s * (s - a) * (s - b) * (s - c)); + } + void display() { + System.out.println("A triangle with side lengths " + a + " units, " + b + " units and " + c + " units has the area of " + area() + " square units."); + } } class AbstractShapeCalculations { @@ -65,12 +65,12 @@ class AbstractShapeCalculations { Shape s; s = new Rectangle(length, width); s.display(); - System.out.println("Circle"); + System.out.println("\nCircle"); System.out.print(" Enter radius: "); double radius = sc.nextDouble(); s = new Circle(radius); s.display(); - System.out.println("Triangle"); + System.out.println("\nTriangle"); System.out.print(" Enter three side lengths: "); double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(); s = new Triangle(a, b, c); diff --git a/java/code/CircularBaseExample.java b/java/code/CircularBaseExample.java index c9a21fe..77b4a74 100644 --- a/java/code/CircularBaseExample.java +++ b/java/code/CircularBaseExample.java @@ -80,7 +80,7 @@ public class CircularBaseExample { System.out.println("Cylinder volume: " + cylinder.calVolume()); } catch (InputMismatchException e) { System.err.println("Invalid input. Please enter numerical values only."); - System.exit(1); + System.exit(1); } } } diff --git a/java/code/EmployeeDisplay.java b/java/code/EmployeeDisplay.java index 6642540..5aab9d0 100644 --- a/java/code/EmployeeDisplay.java +++ b/java/code/EmployeeDisplay.java @@ -1,58 +1,55 @@ class Employee { - int id; - String name; - Employee(int eid, String ename) { - id = eid; - name = ename; - } - public String toString() { - return "Employee named \"" + name + "\" with ID " + id + "."; - } + int id; + String name; + Employee(int eid, String ename) { + id = eid; + name = ename; + } + public String toString() { + return "Employee named \"" + name + "\" with ID " + id + "."; + } } class Scientist extends Employee { - int number_of_publications, experience; - Scientist(int sid, String sname, int nopubs, int yexp) { - super(sid, sname); - number_of_publications = nopubs; - experience = yexp; - } - public String toString() { - return "Scientist named \"" + name + "\" with ID " + id + - ", " + number_of_publications + " publications and " + - experience + " years of experience" + "."; - } + int number_of_publications, experience; + Scientist(int sid, String sname, int nopubs, int yexp) { + super(sid, sname); + number_of_publications = nopubs; + experience = yexp; + } + public String toString() { + return "Scientist named \"" + name + "\" with ID " + id + ", " + + number_of_publications + " publications and " + experience + + " years of experience" + "."; + } } class DScientist extends Scientist { - String[] award; - DScientist(int sid, String sname, int nopubs, int yexp, - String[] dsaward) { - super(sid, sname, nopubs, yexp); - award = dsaward; - } - public String toString() { - return "Distinguished scientist named \"" + name + - "\" with ID " + id + - ", " + number_of_publications + " publications, " + - experience + " years of experience and awardee of " + - String.join(", ", award) + "."; - } + String[] award; + DScientist(int sid, String sname, int nopubs, int yexp, + String[] dsaward) { + super(sid, sname, nopubs, yexp); + award = dsaward; + } + public String toString() { + return "Distinguished scientist named \"" + name + "\" with ID " + id + + ", " + number_of_publications + " publications, " + experience + + " years of experience and awardee of " + String.join(", ", award) + "."; + } } class EmployeeDisplay { - public static void main(String args[]) { - Employee e; - e = new Employee(87416846, "Kim Ji-hyung"); - System.out.println(e); - e = new Scientist(14534, "Daniel Lemire", 80, 25); - System.out.println(e); - e = new DScientist(11, "Donald Ervin Knuth", 185, 60, - new String[] { "Grace Murray Hopper Award (1971)", - "Turing Award (1974)", "National Medal of Science (1979)", - "John von Neumann Medal (1995)", "Harvey Prize (1995)", - "Kyoto Prize (1996)", "Faraday Medal (2011)"}); - System.out.println(e); - } + public static void main(String args[]) { + Employee e; + e = new Employee(87416846, "Kim Ji-hyung"); + System.out.println(e); + e = new Scientist(14534, "Daniel Lemire", 80, 25); + System.out.println(e); + e = new DScientist(11, "Donald Ervin Knuth", 185, 60, + new String[] { "Grace Murray Hopper Award (1971)", "Turing Award (1974)", + "National Medal of Science (1979)", "John von Neumann Medal (1995)", + "Harvey Prize (1995)", "Kyoto Prize (1996)", "Faraday Medal (2011)"}); + System.out.println(e); + } } diff --git a/java/code/ThreadsExample.java b/java/code/ThreadsExample.java index 33ad571..e300666 100644 --- a/java/code/ThreadsExample.java +++ b/java/code/ThreadsExample.java @@ -1,7 +1,7 @@ class NumberRunnable implements Runnable { public void run() { for (int i = 1; i <= 10; i++) { - System.out.println(Thread.currentThread().getId() + " - " + i); + System.out.println("Runnable Thread " + Thread.currentThread().getId() + " - Tick " + i); } } } @@ -9,7 +9,7 @@ class NumberRunnable implements Runnable { class NumberThread extends Thread { public void run() { for (int i = 1; i <= 10; i++) { - System.out.println(Thread.currentThread().getId() + " - " + i); + System.out.println("Subclass Thread " + Thread.currentThread().getId() + " - Tick " + i); } } } diff --git a/java/contents.typ b/java/contents.typ new file mode 100644 index 0000000..5a5475f --- /dev/null +++ b/java/contents.typ @@ -0,0 +1,49 @@ +#import "@preview/tablex:0.0.7": tablex, cellx +#import "/template.typ": apply, signature, list-of-assignments, apply-page-borders + +#apply(page-numbering: "(i)" , [ + #let heading-format(content) = cellx(align: center + horizon, content) + #let column-alignments = (right, auto, center + horizon, center + horizon, auto) + #let preprocess-alist(assignment-list, last-page-number) = { + let index = 0 + let last-index = assignment-list.len() - 1 + let page-number-list = () + while index < last-index { + let item = assignment-list.at(index) + let next-item = assignment-list.at(index + 1) + let starting-page-number = item.page-number + let finishing-page-number = next-item.page-number - 1 + page-number-list.push((starting-page-number, finishing-page-number)) + index = index + 1 + } + page-number-list.push((assignment-list.at(last-index).page-number, last-page-number)) + let new-assignment-list = () + index = 0 + for (start, end) in page-number-list { + let page-number = if start == end [#start] else [#start - #end] + let assignment = assignment-list.at(index) + let serial-number = [#{assignment.number}. ] + let description = stack(dir: ltr, v(5em), assignment.description) + let item = (serial-number, description, page-number, assignment.date, []) + new-assignment-list.push(item) + index = index + 1 + } + new-assignment-list + } + #list-of-assignments((assignment-list, last-page-number) => { + counter(page).update(1) + align(center, [== Contents]) + tablex( + columns: (3em, 1fr, 4em, 6em, 11em), + stroke: 1pt + gray, + repeat-header: true, + map-cols: (i, cells) => (cells.first(), ..cells.slice(1).map(cell => (..cell, align: column-alignments.at(i)))), + heading-format[*Sl.* \ *No.*], heading-format[*Description*], heading-format[*Page No.*], heading-format[*Date*], heading-format[*Teacher’s* \ *Signature*], + ..preprocess-alist(assignment-list, last-page-number).flatten(), + ) + signature() + }) +]) + +#colbreak() + diff --git a/java/cover.tpl.typ b/java/cover.tpl.typ new file mode 100644 index 0000000..de38f47 --- /dev/null +++ b/java/cover.tpl.typ @@ -0,0 +1,37 @@ +#import "@preview/tablex:0.0.7": tablex, cellx +#import "/template.typ": apply + +#let cover(page-count) = apply(page-numbering: (..nums) => {}, [ + #set align(center) + #v(1in) + #set text(font: "Hanken Grotesk") + #set text(size: 40pt) + University of --- + #v(0.5in) + #set text(size: 30pt) + B.Sc. Honours Semester V --- \ + Examination 20--- + #v(0.35in) + #set text(font: "Inter", size: 25pt) + Practical Assignments \ _on_ \ Object-oriented programming Lab \ using Java + #v(1in) + #set text(font: "Hanken Grotesk", size: 20pt) + #tablex( + columns: 2, + stroke: none, + align: left, + gutter: 5pt, + [*Roll No.:*], [---], + [*Registration No.:*], [---], + [*Subject Code:*], [---], + [*Paper Code:*], [---], + [*Number of pages:*], page-count + ) +]) + +#locate(loc => { + let last-page-number = counter(page).final(loc).first() + cover(last-page-number) + colbreak() +}) + diff --git a/java/output/AbstractShapeCalculations.typ b/java/output/AbstractShapeCalculations.typ new file mode 100644 index 0000000..c893f73 --- /dev/null +++ b/java/output/AbstractShapeCalculations.typ @@ -0,0 +1,19 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java AbstractShapeCalculations +Calculate and display shapes + +Rectangle + Enter length: 12 + Enter width: 8 +A rectangle with length 12.0 units and width 8.0 units has the area of 96.0 square units. + +Circle + Enter radius: 9 +A circle with radius 9.0 units has the area of 254.46900494077323 square units. + +Triangle + Enter three side lengths: 5 12 13 +A triangle with side lengths 5.0 units, 12.0 units and 13.0 units has the area of 30.0 square units. +```] + diff --git a/java/output/CircularBaseExample.typ b/java/output/CircularBaseExample.typ new file mode 100644 index 0000000..03769ae --- /dev/null +++ b/java/output/CircularBaseExample.typ @@ -0,0 +1,17 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java CircularBaseExample +Enter radius of Cone: 5 +Enter height of Cone: 8 +Enter radius of Cylinder: 7 +Enter height of Cylinder: 6 + +Cone base area: 78.53981633974483 +Cone total area: 204.20352248333654 +Cone volume: 209.43951023931953 + +Cylinder base area: 153.93804002589985 +Cylinder total area: 571.7698629533423 +Cylinder volume: 923.6282401553991 +```] + diff --git a/java/output/CustomExceptionExample.typ b/java/output/CustomExceptionExample.typ new file mode 100644 index 0000000..1e15b79 --- /dev/null +++ b/java/output/CustomExceptionExample.typ @@ -0,0 +1,13 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java CustomExceptionExample +Enter an integer: 23 +You entered: 23 +$ java CustomExceptionExample +Enter an integer: -4 +Number cannot be less than zero +$ java CustomExceptionExample +Enter an integer: abc +Input must be an integer. +```] + diff --git a/java/output/InnerClassExample.typ b/java/output/InnerClassExample.typ new file mode 100644 index 0000000..9c1abb1 --- /dev/null +++ b/java/output/InnerClassExample.typ @@ -0,0 +1,8 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java InnerClassExample +Outer member: 10 +Inner member: 20 +Inner member (through object): 20 +```] + diff --git a/java/output/InterfaceExample.typ b/java/output/InterfaceExample.typ new file mode 100644 index 0000000..4d2958b --- /dev/null +++ b/java/output/InterfaceExample.typ @@ -0,0 +1,11 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java InterfaceExample +Implementation of method1 +Implementation of method2 +Implementation of method3 +Implementation of method4 +Implementation of newMethod +Concrete method in the concrete class +```] + diff --git a/java/output/PackageExample.typ b/java/output/PackageExample.typ new file mode 100644 index 0000000..d80d8ab --- /dev/null +++ b/java/output/PackageExample.typ @@ -0,0 +1,8 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java PackageExample +MethodTwo in ClassTwo +Variable from ClassOne: 10 +From methodOne in ClassOne +```] + diff --git a/java/output/PackageInterfaceExample.typ b/java/output/PackageInterfaceExample.typ new file mode 100644 index 0000000..37a528d --- /dev/null +++ b/java/output/PackageInterfaceExample.typ @@ -0,0 +1,8 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java PackageInterfaceExample +Implementation of methodOne +Implementation of methodTwo +Implementation of methodThree +```] + diff --git a/java/output/RuntimePolymorphismExample.typ b/java/output/RuntimePolymorphismExample.typ new file mode 100644 index 0000000..132ff7f --- /dev/null +++ b/java/output/RuntimePolymorphismExample.typ @@ -0,0 +1,9 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java RuntimePolymorphismExample +Drawing a Circle +Drawing a Square +Calculating Circle Area +Calculating Square Area +```] + diff --git a/java/output/Template.typ b/java/output/Template.typ new file mode 100644 index 0000000..c1fa1d4 --- /dev/null +++ b/java/output/Template.typ @@ -0,0 +1,4 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +```] + diff --git a/java/output/ThreadsExample.typ b/java/output/ThreadsExample.typ new file mode 100644 index 0000000..b6dbc81 --- /dev/null +++ b/java/output/ThreadsExample.typ @@ -0,0 +1,73 @@ +#import "/template.typ": highlight-output +#v(0.8em) +#columns(2)[ +#highlight-output[``` +$ java ThreadsExample +Subclass Thread 13 - Tick 1 +Subclass Thread 13 - Tick 2 +Subclass Thread 13 - Tick 3 +Subclass Thread 13 - Tick 4 +Subclass Thread 13 - Tick 5 +```] +#highlight-output[``` +Subclass Thread 13 - Tick 6 +Subclass Thread 13 - Tick 7 +Subclass Thread 13 - Tick 8 +Subclass Thread 13 - Tick 9 +Subclass Thread 13 - Tick 10 +Runnable Thread 18 - Tick 1 +```] +#highlight-output[``` +Subclass Thread 14 - Tick 1 +Runnable Thread 18 - Tick 2 +Runnable Thread 17 - Tick 1 +Subclass Thread 14 - Tick 2 +Runnable Thread 17 - Tick 2 +Runnable Thread 16 - Tick 1 +Subclass Thread 15 - Tick 1 +Runnable Thread 16 - Tick 2 +Runnable Thread 17 - Tick 3 +Runnable Thread 17 - Tick 4 +Runnable Thread 17 - Tick 5 +Runnable Thread 17 - Tick 6 +Runnable Thread 17 - Tick 7 +Runnable Thread 17 - Tick 8 +Runnable Thread 17 - Tick 9 +Runnable Thread 17 - Tick 10 +Subclass Thread 14 - Tick 3 +Runnable Thread 18 - Tick 3 +Runnable Thread 16 - Tick 3 +Subclass Thread 14 - Tick 4 +Subclass Thread 14 - Tick 5 +Subclass Thread 14 - Tick 6 +Subclass Thread 14 - Tick 7 +Subclass Thread 14 - Tick 8 +Subclass Thread 14 - Tick 9 +```] +#colbreak() +#highlight-output[``` +Subclass Thread 14 - Tick 10 +Runnable Thread 16 - Tick 4 +Runnable Thread 16 - Tick 5 +Runnable Thread 16 - Tick 6 +Subclass Thread 15 - Tick 2 +Runnable Thread 16 - Tick 7 +Runnable Thread 18 - Tick 4 +Runnable Thread 18 - Tick 5 +Runnable Thread 18 - Tick 6 +Runnable Thread 18 - Tick 7 +Runnable Thread 18 - Tick 8 +Runnable Thread 16 - Tick 8 +Subclass Thread 15 - Tick 3 +Runnable Thread 16 - Tick 9 +Subclass Thread 15 - Tick 4 +Runnable Thread 18 - Tick 9 +Subclass Thread 15 - Tick 5 +Runnable Thread 16 - Tick 10 +Subclass Thread 15 - Tick 6 +Subclass Thread 15 - Tick 7 +Subclass Thread 15 - Tick 8 +Subclass Thread 15 - Tick 9 +Subclass Thread 15 - Tick 10 +Runnable Thread 18 - Tick 10 +```]] diff --git a/java/prelude.typ b/java/prelude.typ index 09e239e..ed93c03 100644 --- a/java/prelude.typ +++ b/java/prelude.typ @@ -1,45 +1,8 @@ #import "@preview/tablex:0.0.7": tablex, cellx -#import "/template.typ": * +#import "/template.typ": apply, signature, list-of-assignments, apply-page-borders -#apply(page-numbering: "(i)" , [ - #align(center, [== Contents]) - #let heading-format(content) = cellx(align: center + horizon, content) - #let column-alignments = (right, auto, center + horizon, center + horizon, auto) - #let preprocess-alist(assignment-list, last-page-number) = { - let index = 0 - let last-index = assignment-list.len() - 1 - let page-number-list = () - while index < last-index { - let item = assignment-list.at(index) - let next-item = assignment-list.at(index + 1) - let starting-page-number = item.page-number - let finishing-page-number = next-item.page-number - 1 - page-number-list.push((starting-page-number, finishing-page-number)) - index = index + 1 - } - page-number-list.push((assignment-list.at(last-index).page-number, last-page-number)) - let new-assignment-list = () - index = 0 - for (start, end) in page-number-list { - let page-number = if start == end [#start] else [#start - #end] - let assignment = assignment-list.at(index) - let item = ([#{assignment.number}. ], assignment.description, page-number, assignment.date, []) - new-assignment-list.push(item) - index = index + 1 - } - new-assignment-list - } - #list-of-assignments((assignment-list, last-page-number) => - tablex( - columns: (3em, 1fr, 4em, 6em, 11em), - stroke: 1pt + gray, - map-cols: (i, cells) => (cells.first(), ..cells.slice(1).map(cell => (..cell, align: column-alignments.at(i)))), - heading-format[*Sl.* \ *No.*], heading-format[*Description*], heading-format[*Page No.*], heading-format[*Date*], heading-format[*Teacher’s* \ *Signature*], - ..preprocess-alist(assignment-list, last-page-number).flatten(), - )) -]) - -#colbreak() +#include "/cover.typ" +#include "/contents.typ" #counter(page).update(0) diff --git a/java/state.sql b/java/state.sql index d0c22bd..c974626 100644 --- a/java/state.sql +++ b/java/state.sql @@ -18,17 +18,17 @@ INSERT INTO `assignments` VALUES ('StudentsArray', 1, 1, 1, 1), ('BankAccountExample', 1, 1, 1, 1), ('ShapeAreaCalculations', 1, 1, 1, 0), - ('AbstractShapeCalculations', 1, 0, 0, 0), + ('AbstractShapeCalculations', 1, 1, 1, 0), ('ParentWithTwoChildren', 1, 1, 1, 0), - ('CuboidCalculations', 1, 1, 0, 0), - ('EmployeeDisplay', 1, 1, 0, 0), - ('CircularBaseExample', 1, 0, 0, 0), - ('InnerClassExample', 1, 0, 0, 0), - ('InterfaceExample', 1, 0, 0, 0), - ('RuntimePolymorphismExample', 1, 0, 0, 0), - ('PackageExample', 1, 0, 0, 0), - ('PackageInterfaceExample', 1, 0, 0, 0), - ('CustomExceptionExample', 1, 0, 0, 0), - ('ThreadsExample', 1, 0, 0, 0); + ('CuboidCalculations', 1, 1, 1, 0), + ('EmployeeDisplay', 1, 1, 1, 0), + ('CircularBaseExample', 1, 1, 1, 0), + ('InnerClassExample', 1, 1, 1, 0), + ('InterfaceExample', 1, 1, 1, 0), + ('RuntimePolymorphismExample', 1, 1, 1, 0), + ('PackageExample', 1, 1, 1, 0), + ('PackageInterfaceExample', 1, 1, 1, 0), + ('CustomExceptionExample', 1, 1, 1, 0), + ('ThreadsExample', 1, 1, 1, 0); -- SELECT * FROM `assignments`; diff --git a/java/template.typ b/java/template.typ index ecaa5da..192ce1c 100644 --- a/java/template.typ +++ b/java/template.typ @@ -31,7 +31,7 @@ } /* Draws page border around the provided content, taking an optional function to be called at the footer. */ -#let apply-page-borders(body, ..font-options, footer-special-func: none, page-numbering: none) = { +#let apply-page-borders(body, font-options: (), footer-special-func: none, page-numbering: none) = { let page-margin = (left: 0.75in, right: 0.25in, top: 0.25in, bottom: 0.25in) let margin = (left: 0.65in, right: 0.15in, top: 1.5em, bottom: 1.5em) let page-border-thickness = 1.25pt @@ -74,12 +74,19 @@ show raw.where(block: true): it => align(left, it) set list(marker: ([$square.filled.tiny$], [--])) set par(leading: 0.5em) - apply-page-borders(body, ..body-font-settings, footer-special-func: signature-footer, page-numbering: page-numbering) + apply-page-borders(body, font-options: body-font-settings, footer-special-func: signature-footer, page-numbering: page-numbering) } -#let scos(name, pad: none) = { +#let scos(name, pad: none, include-before: ()) = { v(1em) [=== Source Code] + if include-before.len() != 0 { + for file in include-before { + [==== File: #raw(lang: "text", file + ".java")] + highlight-code-file("/code/" + file + ".java") + } + [==== Main file: #raw(lang: "text", name + "java")] + } highlight-code-file("/code/" + name + ".java") if pad != none { v(pad) } [=== Output] @@ -90,10 +97,10 @@ #let alist = state("assignment-list", ()) -#let list-of-assignments(cb) = locate(loc => { - let l = alist.final(loc) - let lpno = counter(page).final(loc).first() - cb(l, lpno) +#let list-of-assignments(contents) = locate(loc => { + let assignment-list = alist.final(loc) + let last-page-number = counter(page).final(loc).first() + contents(assignment-list, last-page-number) }) #let list-of-dates = ([11/09/2023], [16/09/2023], [26/09/2023], [04/10/2023], [09/10/2023], [10/10/2023], [13/10/2023], [16/10/2023], [21/11/2023], [28/11/2023], [02/12/2023], [04/12/2023], [04/12/2023], [05/12/2023], [19/12/2023], [02/01/2024], [08/01/2024], [09/01/2024], [15/01/2024], [18/01/2024]) @@ -114,8 +121,7 @@ ] else [ == #text(weight: 500, [Program statement:]) #text(weight: 400, description) ] - let desc = if pad { [#description \ \ ] } else { description } - locate(loc => alist.update(lst => (..lst, (number: number, description: desc, page-number: counter(page).at(loc).first(), date: date)))) + locate(loc => alist.update(lst => (..lst, (number: number, description: description, page-number: counter(page).at(loc).first(), date: date)))) } ]) diff --git a/java/text/AbstractShapeCalculations.typ b/java/text/AbstractShapeCalculations.typ new file mode 100644 index 0000000..83d3999 --- /dev/null +++ b/java/text/AbstractShapeCalculations.typ @@ -0,0 +1,36 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.6em) + +#assignment(14, block: true)[ + Write a program to create an abstract class `Shape` with two abstract methods, `area()` and `display()` and make three concrete derived classes `Rectangle`, `Circle` and `Triangle` which can calculate area and display them seperately. +] + +#scos("AbstractShapeCalculations") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `Shape` abstract class: + - `abstract double area()`: To be implemented by subclasses for calculating the area. + - `abstract void display()`: To be implemented by subclasses for displaying shape details. + +- The `Rectangle` class extends `Shape`: + - `double area()`: Calculates the area of a rectangle using its length and width. + - `void display()`: Displays details of the rectangle. + +- The `Circle` class extends `Shape`: + - `double area()`: Calculates the area of a circle using its radius. + - `void display()`: Displays details of the circle. + +- The `Triangle` class extends `Shape`: + - `double area()`: Calculates the area of a triangle using its side lengths. + - `double perimeter()`: Calculates the perimeter of a triangle. + - `void display()`: Displays details of the triangle. + +- The `AbstractShapeCalculations` class contains the main program: + - `public static void main(String[])`: Implements a menu-driven program for calculating and displaying areas of different shapes (Rectangle, Circle, Triangle) based on user input. Utilizes abstract classes and polymorphism for handling various shapes and their respective calculations. + +#signature() diff --git a/java/text/CircularBaseExample.typ b/java/text/CircularBaseExample.typ new file mode 100644 index 0000000..2a9c495 --- /dev/null +++ b/java/text/CircularBaseExample.typ @@ -0,0 +1,37 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment(18, block: true)[ + Write a program to create a class named `CircularBase` containing a method `getArea()` to calculate the base area, an interface named `_3dShape` with two methods `calArea()` and `calVolume()`, two subclasses of `CircularBase` named `Cone` and `Cylinder` implementing `_3dShape`; calculate the base area and volume of these shapes with the help of `calArea()` method. +] + +#scos("CircularBaseExample") + +=== Discussion + +#skind[Interfaces, classes and methods implemented in the program] + +- The `ThreeDimensionalShape` interface: + - `double calArea()`: Calculates the surface area of a three-dimensional shape. + - `double calVolume()`: Calculates the volume of a three-dimensional shape. + +- The `CircularBase` class: + - Constructor `CircularBase(double)`: Initializes a shape with a circular base using the provided radius. + - `double getArea()`: Calculates the area of the circular base. + +- The `Cone` class extends `CircularBase` and implements `ThreeDimensionalShape`: + - Constructor `Cone(double, double)`: Initializes a cone with a circular base, taking radius and height. + - `double calArea()`: Overrides the interface method to calculate the total surface area of the cone. + - `double calVolume()`: Overrides the interface method to calculate the volume of the cone. + +- The `Cylinder` class extends `CircularBase` and implements `ThreeDimensionalShape`: + - Constructor `Cylinder(double, double)`: Initializes a cylinder with a circular base, taking radius and height. + - `double calArea()`: Overrides the interface method to calculate the total surface area of the cylinder. + - `double calVolume()`: Overrides the interface method to calculate the volume of the cylinder. + +- The `CircularBaseExample` class contains the main program: + - `public static void main(String[])`: Takes user input for the dimensions of a cone and a cylinder, creates instances of these shapes, and displays their base area, total surface area, and volume. + +#signature() diff --git a/java/text/CuboidCalculations.typ b/java/text/CuboidCalculations.typ new file mode 100644 index 0000000..ce1ef82 --- /dev/null +++ b/java/text/CuboidCalculations.typ @@ -0,0 +1,26 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment(16)[ + Write a program to create a base class named `Rectangle` and another class named `Cuboid` deriving `Rectangle` overloading the constructors and print surface area and volume of a `Cuboid` object. +] +#v(2em) +#scos("CuboidCalculations") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `Rectangle` class: + - `int area()`: Calculates the area of a rectangle based on its length and breadth. + +- The `Cuboid` class extends `Rectangle`: + - `int surfaceArea()`: Calculates the surface area of a cuboid based on its length, breadth, and height. + - `int volume()`: Calculates the volume of a cuboid based on its length, breadth, and height. + +- The `CuboidCalculations` class contains the main program: + - `public static void main(String[])`: Creates an instance of `Cuboid`, calculates and displays its surface area and volume. + +#signature() diff --git a/java/text/CustomExceptionExample.typ b/java/text/CustomExceptionExample.typ new file mode 100644 index 0000000..4b653e8 --- /dev/null +++ b/java/text/CustomExceptionExample.typ @@ -0,0 +1,23 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.75em) + +#assignment(24, block: true)[ + Write a program that prompts the user for an integer input, and if the entered value is less than zero, throw a custom exception; additionally, handle exceptions for input of non-integer data types. +] + +#scos("CustomExceptionExample") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- Class `NegativeNumberException` (extends `Exception`): Custom exception class for negative numbers. + - Constructor `public NegativeNumberException(String)`: Constructs an Exception object to be thrown using the provided message string. + +- Main program: + - Class `CustomExceptionExample`: + - `public static void main(String[])`: Contains the main program demonstrating custom exception handling for negative numbers and handling input mismatches. + +#signature() diff --git a/java/text/EmployeeDisplay.typ b/java/text/EmployeeDisplay.typ new file mode 100644 index 0000000..d7d0253 --- /dev/null +++ b/java/text/EmployeeDisplay.typ @@ -0,0 +1,31 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.6em) + +#assignment(17, block: true)[ + Write a program to create a class `Employee` with instance variables `name` and `id`; a subclass of `Employee` named `Scientist` with instance variables `no_of_publication` and `experience`; and its subclass named `DScientist` with instance variable `award`; implement the `public String toString() { }` method in each class to describe about its object with the member variables and from `main()` method create an object of each class and print each object. +] + +#scos("EmployeeDisplay") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `Employee` class: + - Constructor `Employee(int, String)`: Initializes an employee with ID and name. + - `public String toString()`: Returns a string representation of the employee details. + +- The `Scientist` class extends `Employee`: + - Constructor `Scientist(int, String, int, int)`: Initializes a scientist with additional attributes like the number of publications and years of experience. + - `public String toString()`: Returns a string representation of the scientist details. + +- The `DScientist` class extends `Scientist`: + - Constructor `DScientist(int, String, int, int, String[])`: Initializes a distinguished scientist with additional awards. + - `public String toString()`: Returns a string representation of the distinguished scientist details. + +- The `EmployeeDisplay` class contains the main program: + - `public static void main(String[])`: Creates instances of different employee types (Employee, Scientist, Distinguished Scientist) and displays their details. + +#signature() diff --git a/java/text/InnerClassExample.typ b/java/text/InnerClassExample.typ new file mode 100644 index 0000000..796eac4 --- /dev/null +++ b/java/text/InnerClassExample.typ @@ -0,0 +1,28 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment(19, block: true)[ + Write a program to create a class containing an inner class to show that inner class can use members of outer class directly, but the outer class can use members of inner class only through its objects. Check the name of the inner class file created when it was compiled. +] +#v(-1em) +#scos("InnerClassExample") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `OuterClass` class: + - Private field `int outerMember`: Represents a member variable in the outer class. + - The `InnerClass` class: + - Private field `int innerMember`: Represents a member variable in the inner class. + - Method `void accessMembers()`: Prints the values of both outer and inner members. + - Method `void accessInnerMembers()`: Creates an instance of the inner class and prints the value of the inner member through the object. + +- The `InnerClassExample` class contains the main program: + - `public static void main(String[])`: Demonstrates the usage of outer and inner classes by creating instances and accessing their members. + +/ Note : The file name of the (bytecode) compiled class file corresponding to the inner class is ```text OuterClass$InnerClass.class```. + +#signature() diff --git a/java/text/InterfaceExample.typ b/java/text/InterfaceExample.typ new file mode 100644 index 0000000..d9955fd --- /dev/null +++ b/java/text/InterfaceExample.typ @@ -0,0 +1,39 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.5em) + +#assignment(20, block: true)[ + Write a program in which create two interfaces, each with two methods, inherit a new interface from the two, adding a new method, create a class by implementing the new interface and also inheriting a concrete class. In the `main()` method, create an object of the derived class and call these methods. +] + +#scos("InterfaceExample") + +=== Discussion + +#skind[Interfaces, classes and methods implemented in the program] + +- Interface `Interface1`: + - `void method1()`: Declares a method. + - `void method2()`: Declares another method. + +- Interface `Interface2`: + - `void method3()`: Declares a method. + - `void method4()`: Declares another method. + +- Interface `NewInterface` (extends `Interface1` and `Interface2`): + - Inherits methods from `Interface1` and `Interface2`. + - `void newMethod()`: Declares a new method. + +- Class `ConcreteClass`: + - Method `void concreteMethod()`: Declares a method. + +- Class `DerivedClass` (extends `ConcreteClass` and implements `NewInterface`): + - Implements methods from `Interface1`, `Interface2`, and `NewInterface`. + - Method `void concreteMethod()`: Inherits and overrides a method. + +- Class `InterfaceExample`: + - `public static void main(String[])`: Contains the main program, creating an instance of `DerivedClass` and calling various methods. + + +#signature() diff --git a/java/text/PackageExample.typ b/java/text/PackageExample.typ new file mode 100644 index 0000000..985ad34 --- /dev/null +++ b/java/text/PackageExample.typ @@ -0,0 +1,28 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.65em) + +#assignment(22, block: true)[ + Create a program that defines a class with variables and methods in the `pOne` package, extends this class in another package `pTwo`, and within the method of the `pTwo` class, access the variables and methods of the original class in `pOne`; finally, from the `main()` method in the working directory, access the members of the second class. +] + +#scos("PackageExample", include-before: ("pOne/ClassOne", "pTwo/ClassTwo")) + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- Package `pOne`: + - Class `ClassOne`: + - `protected int variableOne`: A protected variable. + - `public void methodOne()`: A public method. + +- Package `pTwo`: + - Class `ClassTwo` (extends `pOne.ClassOne`): + - `public void methodTwo()`: A public method that calls `methodOne()` and accesses `variableOne` from `ClassOne`. + +- Class `PackageExample`: + - `public static void main(String[])`: Contains the main program creating an object of `ClassTwo` and calling `methodTwo()`. + +#signature() diff --git a/java/text/PackageInterfaceExample.typ b/java/text/PackageInterfaceExample.typ new file mode 100644 index 0000000..639d88b --- /dev/null +++ b/java/text/PackageInterfaceExample.typ @@ -0,0 +1,32 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.6em) + +#assignment(23, block: true)[ + Write a program that defines an interface with three methods in the `pkgOne` package, implements the interface in a class within the `pkgTwo` package, and from the `main()` method in the working directory, instantiate the class and invoke the interface methods. +] + +#scos("PackageInterfaceExample", include-before: ("pkgOne/MyInterface", "pkgTwo/MyClass")) + +=== Discussion + +#skind[Interfaces, classes and methods implemented in the program] + +- Package `pkgOne`: + - Interface `MyInterface`: + - `void methodOne()`: An interface method. + - `void methodTwo()`: An interface method. + - `void methodThree()`: An interface method. + +- Package `pkgTwo`: + - Class `MyClass` (implements `pkgOne.MyInterface`): + - `public void methodOne()`: Implementation of `methodOne` from `MyInterface`. + - `public void methodTwo()`: Implementation of `methodTwo` from `MyInterface`. + - `public void methodThree()`: Implementation of `methodThree` from `MyInterface`. + +- Main program: + - Class `PackageInterfaceExample`: + - `public static void main(String[])`: Contains the main program creating an object of `MyClass` (which implements `MyInterface`) and calling its methods. + +#signature() diff --git a/java/text/RuntimePolymorphismExample.typ b/java/text/RuntimePolymorphismExample.typ new file mode 100644 index 0000000..1235c27 --- /dev/null +++ b/java/text/RuntimePolymorphismExample.typ @@ -0,0 +1,31 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment(21)[ + Write a program, wherein create an Interface and create two sub-classes implementing the interface to show the functionalities of runtime polymorphism (_a.k.a._ dynamic method dispatch) using them. +] + +#scos("RuntimePolymorphismExample") + +=== Discussion + +#skind[Interfaces, classes and methods implemented in the program] + +- Interface `Shape`: + - `void draw()`: Declares a method. + +- Class `Circle` (implements `Shape`): + - Implements the `draw()` method from the `Shape` interface. + - Method `void calculateArea()`: Declares a specific method for calculating the area of a circle. + +- Class `Square` (implements `Shape`): + - Implements the `draw()` method from the `Shape` interface. + - Method `void calculateArea()`: Declares a specific method for calculating the area of a square. + +- Class `RuntimePolymorphismExample`: + - `public static void main(String[])`: Contains the main program demonstrating runtime polymorphism by creating objects of subclasses (`Circle` and `Square`) and calling the `draw()` method dynamically. + + +#signature() diff --git a/java/text/ThreadsExample.typ b/java/text/ThreadsExample.typ new file mode 100644 index 0000000..5e1b2c8 --- /dev/null +++ b/java/text/ThreadsExample.typ @@ -0,0 +1,36 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.5em) + +#assignment(25)[ + Write a program in Java to create three threads printing 1 to 10, then implement the program using both inheriting Thread class and implementing Runnable interface. +] + +#scos("ThreadsExample") + +=== Discussion + +#skind[Classes, interfaces and methods used from Java standard library] + +- `java.lang.Runnable` interface: + - `void run()`: The method to be implemented by the class that implements this interface. + +- `java.lang.Thread` class: + - `void start()`: Causes this thread to begin execution. + - `static Thread currentThread()`: Returns a reference to the currently executing thread. + - `long getId()`: Returns the identifier of this thread. + +#skind[Classes and methods implemented in the program] + +- Class `NumberRunnable` (implements `Runnable`): + - `public void run()`: Implements the `run` method of the `Runnable` interface, printing numbers. + +- Class `NumberThread` (extends `Thread`): + - `public void run()`: Implements the `run` method of the `Thread` class, printing numbers. + +- Main program: + - Class `ThreadsExample`: + - `public static void main(String[])`: Demonstrates multi-threading using both `Thread` and `Runnable` approaches, creating and starting threads to print numbers. + +#signature() -- cgit 1.4.1-2-gfad0