diff options
author | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-24 16:44:31 +0000 |
---|---|---|
committer | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-24 16:44:31 +0000 |
commit | 6766b6479a1d30680c1eb59df4c4b720eb22ce1c (patch) | |
tree | 19af8079fbed64732f891da09c69356153cc0170 | |
parent | 8fb719f58f91d1f1f187a1db974682fb3736ee05 (diff) | |
download | zadania-6766b6479a1d30680c1eb59df4c4b720eb22ce1c.tar.gz |
Implement Java Assignment #15 and more
java/{code/ParentWithTwoChildren.java, output/ParentWithTwoChildren.typ, text/ParentWithTwoChildren.typ}: Code, output and assignment text for Java assignment #15 java/state.sql: Update for Java assignment #15 java/template.typ: Allow for long program description to be left aligned and justified, instead of centered java/tobedone: Update to show assignment numbers (implicitly `rowid`)
-rw-r--r-- | java/code/ParentWithTwoChildren.java | 60 | ||||
-rw-r--r-- | java/output/ParentWithTwoChildren.typ | 9 | ||||
-rw-r--r-- | java/state.sql | 2 | ||||
-rw-r--r-- | java/template.typ | 11 | ||||
-rw-r--r-- | java/text/ParentWithTwoChildren.typ | 28 | ||||
-rw-r--r-- | java/tobedone | 2 |
6 files changed, 109 insertions, 3 deletions
diff --git a/java/code/ParentWithTwoChildren.java b/java/code/ParentWithTwoChildren.java new file mode 100644 index 0000000..20b04cb --- /dev/null +++ b/java/code/ParentWithTwoChildren.java @@ -0,0 +1,60 @@ +class Parent { + int id; + String name; + String address; + + Parent(int id, String name, String address) { + this.id = id; + this.name = name; + this.address = address; + } + + void display() { + String className = this.getClass().getSimpleName(); + System.out.println(className + ": ID: " + id + ", Name: " + name + ", Address: " + address); + } +} + +class ChildOne extends Parent { + int marks; + + ChildOne(int id, String name, String address, int marks) { + super(id, name, address); + this.marks = marks; + } + + @Override + void display() { + super.display(); + System.out.println(" Marks: " + marks); + } +} + +class ChildTwo extends Parent { + String qualification; + double salary; + + ChildTwo(int id, String name, String address, String qualification, double salary) { + super(id, name, address); + this.qualification = qualification; + this.salary = salary; + } + + @Override + void display() { + super.display(); + System.out.println(". Qualification: " + qualification + ", Salary: " + salary); + } +} + +class ParentWithTwoChildren { + public static void main(String[] args) { + Parent parent1 = new Parent(1, "John Doe", "123 Main St."); + ChildOne child1 = new ChildOne(2, "Jane Doe", "456 Elm St.", 85); + ChildTwo child2 = new ChildTwo(3, "Mike Smith", "789 Oak St.", "B.Tech", 50000); + + parent1.display(); + child1.display(); + child2.display(); + } +} diff --git a/java/output/ParentWithTwoChildren.typ b/java/output/ParentWithTwoChildren.typ new file mode 100644 index 0000000..de253ec --- /dev/null +++ b/java/output/ParentWithTwoChildren.typ @@ -0,0 +1,9 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +Parent: ID: 1, Name: John Doe, Address: 123 Main St. +ChildOne: ID: 2, Name: Jane Doe, Address: 456 Elm St. + Marks: 85 +ChildTwo: ID: 3, Name: Mike Smith, Address: 789 Oak St. + Qualification: B.Tech, Salary: 50000.0 +```] + diff --git a/java/state.sql b/java/state.sql index 62023e0..51d5dc1 100644 --- a/java/state.sql +++ b/java/state.sql @@ -19,7 +19,7 @@ INSERT INTO `assignments` VALUES ('BankAccountExample', 1, 0, 0, 1), ('ShapeAreaCalculations', 1, 0, 0, 0), ('AbstractShapeCalculations', 1, 0, 0, 0), - ('FamilyExample', 0, 0, 0, 0), + ('ParentWithTwoChildren', 1, 1, 1, 0), ('CuboidCalculations', 0, 0, 0, 0), ('EmployeeDisplay', 1, 0, 0, 0), ('CircularBaseExample', 0, 0, 0, 0), diff --git a/java/template.typ b/java/template.typ index 57e2e48..30c0a81 100644 --- a/java/template.typ +++ b/java/template.typ @@ -94,9 +94,18 @@ cb(l, lpno) }) -#let assignment(number, description) = align(center, [ +#let assignment(number, description, block: false) = align(center, [ = #text(weight: 600, [Assignment #number]) + #{ + if block == true [ + #set par(justify: true) + #align(left)[ == #text(weight: 500, [Program statement:]) #text(weight: 400, description) + ] + ] else [ +== #text(weight: 500, [Program statement:]) #text(weight: 400, description) + ] + } #locate(loc => alist.update(lst => (..lst, (number, description, counter(page).at(loc).first(), [])))) ]) diff --git a/java/text/ParentWithTwoChildren.typ b/java/text/ParentWithTwoChildren.typ new file mode 100644 index 0000000..e307cc8 --- /dev/null +++ b/java/text/ParentWithTwoChildren.typ @@ -0,0 +1,28 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.5em) + +#assignment(15, block: true)[ + Write a program to create a class `Parent` having instance variables `id`, `name` and `address`; a class `ChildOne` having instance variables `id`, `name`, `address` and `marks`; another class `ChildTwo` with instance variables `id`, `name`, `address`, `qualification` and `salary`. Design the program using `super` call with proper parameters within each class and define your own method to display values of the member variable and use an object of each class from `main()` to display their properties. +] + +#scos("ParentWithTwoChildren") + +=== Discussion + +#skind[Classes, interfaces and methods used from Java standard library] + +- `java.lang.Object` class: + - `Class<?> getClass()`: Returns the `Class` object representing the class of the object. +- `java.lang.Class<T>` class: + - `String getSimpleName()`: Returns the simple name of the underlying class as a string. + +#skind[Classes and methods implemented in the program] + +- `Parent`: Base class with `id`, `name`, and `address`, displaying all information dynamically using the `display` method. +- `ChildOne`: Inherits from `Parent`, adds `marks`, and displays them after parent data using the `display` method. +- `ChildTwo`: Inherits from `Parent`, adds `qualification` and `salary`, and displays them after parent data using the `display` method. +- `Main`: Creates and showcases `Parent`, `ChildOne`, and `ChildTwo` object details using the `main` method. + +#signature() diff --git a/java/tobedone b/java/tobedone index dd34705..fa631b8 100644 --- a/java/tobedone +++ b/java/tobedone @@ -3,5 +3,5 @@ { cat state.sql printf '.mode column\n' - printf 'SELECT name FROM `assignments` WHERE NOT %s;\n' "${1:-code}" + printf 'SELECT rowid, name FROM `assignments` WHERE NOT %s;\n' "${1:-code}" } | sqlite3 | tail -n +3 |