diff options
author | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-27 11:33:43 +0000 |
---|---|---|
committer | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-27 11:34:11 +0000 |
commit | cd6a9243c056710794549a1ab5d51fbdd082ce2e (patch) | |
tree | f3f4df42bcfa96b9d6ef48da2b33be148adfdd2b /java | |
parent | 1db2841d10c6920eba89f1e55eb3c90aa770ad07 (diff) | |
download | zadania-cd6a9243c056710794549a1ab5d51fbdd082ce2e.tar.gz |
Complete Java assignments #7-13
Diffstat (limited to 'java')
-rw-r--r-- | java/alist.txt | 14 | ||||
-rw-r--r-- | java/code/ShapeAreaCalculations.java | 10 | ||||
-rw-r--r-- | java/output/BankAccountExample.typ | 124 | ||||
-rw-r--r-- | java/output/ComplexCalculations.typ | 20 | ||||
-rw-r--r-- | java/output/FindNumberInArray.typ | 15 | ||||
-rw-r--r-- | java/output/ShapeAreaCalculations.typ | 40 | ||||
-rw-r--r-- | java/output/StudentsArray.typ | 55 | ||||
-rw-r--r-- | java/state.sql | 12 | ||||
-rw-r--r-- | java/text/BankAccountExample.typ | 57 | ||||
-rw-r--r-- | java/text/ComplexCalculations.typ | 15 | ||||
-rw-r--r-- | java/text/FindNumberInArray.typ | 36 | ||||
-rw-r--r-- | java/text/ShapeAreaCalculations.typ | 27 | ||||
-rw-r--r-- | java/text/SingletonExample.typ | 24 | ||||
-rw-r--r-- | java/text/StaticExecution.typ | 4 | ||||
-rw-r--r-- | java/text/StudentsArray.typ | 38 | ||||
-rw-r--r-- | java/text/Template.typ | 20 |
16 files changed, 486 insertions, 25 deletions
diff --git a/java/alist.txt b/java/alist.txt index 2dcc72d..d549684 100644 --- a/java/alist.txt +++ b/java/alist.txt @@ -4,11 +4,11 @@ StringOperations # MatrixOperations # AddTwoNumbers # CylinderCalculations # -ComplexCalculations -FindNumberInArray -StaticExecution -SingletonExample -StudentsArray -BankAccountExample -ShapeAreaCalculations +ComplexCalculations # +FindNumberInArray # +StaticExecution # +SingletonExample # +StudentsArray # +BankAccountExample # +ShapeAreaCalculations # AbstractShapeCalculations diff --git a/java/code/ShapeAreaCalculations.java b/java/code/ShapeAreaCalculations.java index 0d00bff..ca20d39 100644 --- a/java/code/ShapeAreaCalculations.java +++ b/java/code/ShapeAreaCalculations.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.InputMismatchException; class Shape { // Calculates the area of a triangle. @@ -20,11 +21,11 @@ class Shape { } } -class AreaCalc { +class ShapeAreaCalculations { public static void main(String args[]) { - var sc = new Scanner(System.in); + try (var sc = new Scanner(System.in)) { System.out.println("Choose the shape: "); - System.out.println("1. Triangle\n2. Rectangle\n3. Square\n4.Circle"); + System.out.println("1. Triangle\n2. Rectangle\n3. Square\n4. Circle"); System.out.print("Enter your choice: "); var choice = sc.nextInt(); switch (choice) { @@ -55,5 +56,8 @@ class AreaCalc { System.out.println("Invalid choice"); break; } + } catch (InputMismatchException e) { + System.err.println("Invalid number given as input"); + } } } diff --git a/java/output/BankAccountExample.typ b/java/output/BankAccountExample.typ new file mode 100644 index 0000000..779f5ff --- /dev/null +++ b/java/output/BankAccountExample.typ @@ -0,0 +1,124 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java BankAccountExample +Menu-driven program for operating bank accounts: +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 1 +Enter details of bank account: + Name: Lee Heoksoo + Address: Sindang-dong, Seoul + Account No.: 120045387091 + Balance: 71623500 +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 2 +Enter account number: 120045387091 +Customer details: + Name: Lee Heoksoo + Address: Sindang-dong, Seoul + Account Number: 120045387091 + Balance: 71623500.00 + +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 1 +Enter details of bank account: + Name: Axel Schauer + Address: Eidelstedt, Hamburg + Account No.: 582978 + Balance: 36400 +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 4 +Enter account number: 42000 +Could not find a bank account with the given account number. +Try again. + +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 4 +Enter account number: 4200 +Could not find a bank account with the given account number. +Try again. + +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 3 +Enter account number: 12000 +Could not find a bank account with the given account number. +Try again. + +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 5 +Enter account number: 582978 +Are you sure about closing your account (y/n): y +Account closing successful. +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 2 +Enter account number: 582978 +Could not find a bank account with the given account number. +Try again. + +Menu: + 1. Create bank account + 2. Display bank account details + 3. Deposit money + 4. Withdraw money + 5. Close bank account + 6. Exit + +Enter your choice: 6 +Bye. +```] + diff --git a/java/output/ComplexCalculations.typ b/java/output/ComplexCalculations.typ new file mode 100644 index 0000000..1b10fe7 --- /dev/null +++ b/java/output/ComplexCalculations.typ @@ -0,0 +1,20 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java ComplexCalculations +First complex number:- + Enter real part: 2 + Enter imaginary part: -3 +Second complex number:- + Enter real part: 5 + Enter imaginary part: 2 +(2.0 - 3.0i) + (5.0 + 2.0i) = 7.0 - 1.0i +$ java ComplexCalculations +First complex number:- + Enter real part: 1.5 + Enter imaginary part: -6.5 +Second complex number:- + Enter real part: 4.23 + Enter imaginary part: 3.7 +(1.5 - 6.5i) + (4.23 + 3.7i) = 5.73 - 2.8i +```] + diff --git a/java/output/FindNumberInArray.typ b/java/output/FindNumberInArray.typ new file mode 100644 index 0000000..9b71bb4 --- /dev/null +++ b/java/output/FindNumberInArray.typ @@ -0,0 +1,15 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java FindNumberInArray +Enter length: 5 +Enter array elements: 4 7 3 2 9 +Given: An array of numbers with 5 elements: 4.0 7.0 3.0 2.0 9.0 +Enter element to find in array: 7 +The number 7.0 was found in the array at position 2. +$ java FindNumberInArray +Enter length: 4 +Enter array elements: 5 9 0 3 +Given: An array of numbers with 4 elements: 5.0 9.0 0.0 3.0 +Enter element to find in array: 4 +Could not find the number 4.0 in the array. +```] diff --git a/java/output/ShapeAreaCalculations.typ b/java/output/ShapeAreaCalculations.typ new file mode 100644 index 0000000..05f14e6 --- /dev/null +++ b/java/output/ShapeAreaCalculations.typ @@ -0,0 +1,40 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java ShapeAreaCalculations +Choose the shape: +1. Triangle +2. Rectangle +3. Square +4. Circle +Enter your choice: 1 +Enter sides: 6 8 10 +Area of a triangle: 24.0 +$ java ShapeAreaCalculations +Choose the shape: +1. Triangle +2. Rectangle +3. Square +4. Circle +Enter your choice: 2 +Enter length and width: 23 14 +Area of a rectangle: 322 +$ java ShapeAreaCalculations +Choose the shape: +1. Triangle +2. Rectangle +3. Square +4. Circle +Enter your choice: 3 +Enter side length: 7 +Area of a square: 49 +$ java ShapeAreaCalculations +Choose the shape: +1. Triangle +2. Rectangle +3. Square +4. Circle +Enter your choice: 4 +Enter radius: 5 +Area of a square: 78.53981633974483 +```] + diff --git a/java/output/StudentsArray.typ b/java/output/StudentsArray.typ new file mode 100644 index 0000000..358a15b --- /dev/null +++ b/java/output/StudentsArray.typ @@ -0,0 +1,55 @@ +#import "/template.typ": highlight-output +#highlight-output[``` +$ java StudentsArray +Enter the number of students: +2 +Enter the details of the students: +For student 1: Enter the details of the student: +Enter Roll no.: 1 +Enter name: Lee Haseol +Enter stream: Science +For subject 1: Enter details of subject: +Enter title: Physics +Enter theory marks: 94 +For subject 2: Enter details of subject: +Enter title: Chemistry +Enter theory marks: 96 +For subject 3: Enter details of subject: +Enter title: Mathematics +Enter theory marks: 98 +For student 2: Enter the details of the student: +Enter Roll no.: 43 +Enter name: Lin Feng +Enter stream: Arts +For subject 1: Enter details of subject: +Enter title: History +Enter theory marks: 47 +For subject 2: Enter details of subject: +Enter title: Caligraphy +Enter theory marks: 56 +For subject 3: Enter details of subject: +Enter title: Philosophy +Enter theory marks: 72 +Displaying the details of the students: + +For student 1: +Student details: + Roll No.: 1 + Name: Lee Haseol + Stream: Science + Marks obtained in Physics: 94 + Marks obtained in Chemistry: 96 + Marks obtained in Mathematics: 98 + Average percentage: 96.00% + +For student 2: +Student details: + Roll No.: 43 + Name: Lin Feng + Stream: Arts + Marks obtained in History: 47 + Marks obtained in Caligraphy: 56 + Marks obtained in Philosophy: 72 + Average percentage: 58.33% +```] + diff --git a/java/state.sql b/java/state.sql index 24bc8fe..d0c22bd 100644 --- a/java/state.sql +++ b/java/state.sql @@ -11,13 +11,13 @@ INSERT INTO `assignments` VALUES ('MatrixOperations', 1, 1, 1, 1), ('AddTwoNumbers', 1, 1, 1, 1), ('CylinderCalculations', 1, 1, 1, 1), - ('ComplexCalculations', 1, 0, 1, 1), - ('FindNumberInArray', 1, 0, 0, 1), + ('ComplexCalculations', 1, 1, 1, 1), + ('FindNumberInArray', 1, 1, 1, 1), ('StaticExecution', 1, 1, 1, 1), - ('SingletonExample', 1, 1, 0, 1), - ('StudentsArray', 1, 0, 0, 1), - ('BankAccountExample', 1, 0, 0, 1), - ('ShapeAreaCalculations', 1, 0, 0, 0), + ('SingletonExample', 1, 1, 1, 1), + ('StudentsArray', 1, 1, 1, 1), + ('BankAccountExample', 1, 1, 1, 1), + ('ShapeAreaCalculations', 1, 1, 1, 0), ('AbstractShapeCalculations', 1, 0, 0, 0), ('ParentWithTwoChildren', 1, 1, 1, 0), ('CuboidCalculations', 1, 1, 0, 0), diff --git a/java/text/BankAccountExample.typ b/java/text/BankAccountExample.typ new file mode 100644 index 0000000..2cb1cc8 --- /dev/null +++ b/java/text/BankAccountExample.typ @@ -0,0 +1,57 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.5em) + +#assignment(12, block: true)[ + Write a program wherein design a class to represent a bank account. Include the following: + #pad(left: 1em)[ + / Fields : + - Name of the depositor + - Address of the depositor + - Account number + - Balance amount in the account + / Methods : + - To assign initial values + - To deposit an amount + - To withdraw an amount after checking balance + - To display the name, address and balance of a customer. + ] + From `main()` create object and call these methods. +] + +#scos("BankAccountExample") + +=== Discussion + +#skind[Classes, interfaces and methods used from Java standard library] + +- `java.util.ArrayList<E>` class provides a dynamic array implementation, allowing for the flexible storage and manipulation of elements in a resizable list. + - `boolean add(E)`: Adds the specified element to the end of the list. + - `E remove(int)`: Removes the element at the specified position in the list. + - `int size()`: Returns the number of elements in the list. +- `java.util.Scanner` class: + - `String nextLine()`: Scan a line of text from the input stream. + - `long nextLong()`: Scan a `long` value from the input stream. + - `double nextDouble()`: Scan a `double` value from the input stream. + +#skind[Classes and methods implemented in the program] + +- The `BankAccount` class represents a bank account with details like name, address, account number, and balance. + - Constructor `BankAccount(String, String, long, double)`: Create a bank account with the given details. + - `static BankAccount takeInput(Scanner)`: Takes input for a bank account from the user. + - `long getAccountNo()`: Returns the account number of the bank account. + - `double getBalance()`: Returns the balance of the bank account. + - `boolean equals(Object)`: Compares bank accounts based on account number. + - `void deposit(double)`: Deposits money into the account. + - `static String formatBalance(double)`: Formats a balance value for display. + - `void withdraw(double) throws Exception`: Withdraws money from the account, handling exceptions. + - `void display()`: Displays the details of the bank account. + - `static BankAccount lookup(ArrayList<BankAccount>, long)`: Looks up a bank account in the list based on account number. + +- The `BankAccountExample` class contains the main program for interacting with bank accounts through a menu: + - `static void menu()`: Displays a menu of operations. + - `static BankAccount findAccount(Scanner, ArrayList<BankAccount>)`: Finds a bank account based on user input. + - `public static void main(String[])`: Implements a menu-driven program for managing bank accounts, allowing creation, display, deposit, withdrawal, and closure of accounts. + +#signature() diff --git a/java/text/ComplexCalculations.typ b/java/text/ComplexCalculations.typ index b8df211..ffa9304 100644 --- a/java/text/ComplexCalculations.typ +++ b/java/text/ComplexCalculations.typ @@ -1,9 +1,9 @@ #import "/template.typ": * #show: A => apply(A) #set raw(lang: "java-new") -#set par(leading: 0.5em) +#set par(leading: 0.65em) -#assignment(6)[ +#assignment(7)[ Write a program to add two complex numbers using concept of methods returning objects and methods taking objects as parameters. ] @@ -23,9 +23,10 @@ - The `Complex` class objects represents a a complex number. - Constructor `Complex(double, double)`: Create a complex number object from the provided real and imaginary parts. - `public String toString()` method returns the string representation of the complex number. -- The `ComplexOperations` class -- The `CylinderCalculationsCLI` orchestrates a main program with: - - `public static void main(String[])`: Creates a cylinder object with its radius and height given as command-line arguments. -- The `CylinderCalculationsScan` orchestrates a main program with: - - `public static void main(String[])`: Creates a cylinder object with its radius and height taken from user using `java.util.Scanner`. +- The `ComplexOperations` class contains operations that can be performed on `Complex` objects. + - `static Complex add(Complex a, Complex b) `: Returns the sum of two complex numbers provided. +- The `ComplexCalculations` orchestrates a main program with: + - `static Complex takeComplexInput()`: Returns the complex number provided by the user. + - `public static void main(String[])`: Performs addition of two complex numbers. + #signature() diff --git a/java/text/FindNumberInArray.typ b/java/text/FindNumberInArray.typ new file mode 100644 index 0000000..9c6a4bd --- /dev/null +++ b/java/text/FindNumberInArray.typ @@ -0,0 +1,36 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment(8, pad: true)[ + Write a program to find a number from an array of number objects. +] + +#scos("FindNumberInArray") + +#v(1em) + +=== Discussion + +#skind[Classes, interfaces and methods used from Java standard library] + +- `java.util.Scanner` class: + - `int nextInt()`: Scan an `int` value from the input stream. + - `double nextDouble()`: Scan a `double` value from the input stream. +- `java.util.InputMismatchException` is thrown by the methods of `Scanner` class when encountering invalid input. + +#skind[Classes and methods implemented in the program] + +- The `Number` class objects represent a `double` value and provide methods for equality comparison. + - Constructor `Number(double)`: Create a number object for the provided value. + - `double valueOf()`: Returns the underlying number. + - `boolean equals(Object)`: Compares the equality of the number with another object. +- The `NumberArray` class manages an array of `Number` objects. + - Constructor `NumberArray(double[])`: Create a `NumberArray` object from an array of `double` values. + - `int find(Number)`: Find the position of a given `Number` in the array, or return -1 if not found. + - `void display()`: Display the elements of the array. +- The `FindNumberInArray` class contains the main program for finding a number in an array: + - `public static void main(String[])`: Takes user input for the length and elements of an array, creates a `NumberArray`, and finds a specified number in the array. + +#signature() diff --git a/java/text/ShapeAreaCalculations.typ b/java/text/ShapeAreaCalculations.typ new file mode 100644 index 0000000..f34c1a0 --- /dev/null +++ b/java/text/ShapeAreaCalculations.typ @@ -0,0 +1,27 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.6em) + +#assignment(13)[ + Write a program to create a class `Shape` with 4 methods to calculate the areas of triangle, rectangle, square, and circle using method overloading. +] + +#scos("ShapeAreaCalculations") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `Shape` class provides static methods for calculating the area of different geometric shapes: + - `static double area(double a, double b, double c)`: Calculates the area of a triangle using its side lengths. + - `static int area(int length, int width)`: Calculates the area of a rectangle using its length and width. + - `static int area(int sideLength)`: Calculates the area of a square using its side length. + - `static double area(double radius)`: Calculates the area of a circle using its radius. + +- The `ShapeAreaCalculations` class contains the main program for calculating the area of geometric shapes based on user input through a menu-driven interface: + - `static void menu()`: Displays a menu of shape choices. + - `public static void main(String[])`: Implements a menu-driven program for calculating the area of different shapes (_i.e._ triangle, rectangle, square, or circle) based on user input. + + +#signature() diff --git a/java/text/SingletonExample.typ b/java/text/SingletonExample.typ new file mode 100644 index 0000000..84a5cff --- /dev/null +++ b/java/text/SingletonExample.typ @@ -0,0 +1,24 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.4em) + +#assignment(10, reduce-gap: true, pad: true)[ + Write a program to implement a singleton class. +] +#v(-1em) +#scos("SingletonExample") + +=== Discussion + +#skind[Classes and methods implemented in the program] + +- The `Singleton` class implements the Singleton design pattern to ensure a single instance of the class is created. + - `private static Singleton instance`: Private static variable to hold the single instance of the class. + - `private Singleton()`: Private constructor to prevent external instantiation and prints a message when the instance is created. + - `public static Singleton getInstance()`: Public method to retrieve the singleton instance. If the instance does not exist, it creates one and prints a message. + +- The `SingletonExample` class contains the main program to demonstrate the Singleton pattern: + - `public static void main(String[])`: Creates two instances of `Singleton` using the `getInstance()` method and checks if they refer to the same instance. + - If `s1` and `s2` reference the same object, it prints "The singleton instances are identical." +#signature() diff --git a/java/text/StaticExecution.typ b/java/text/StaticExecution.typ index 00b4ed0..d89c8f0 100644 --- a/java/text/StaticExecution.typ +++ b/java/text/StaticExecution.typ @@ -3,10 +3,10 @@ #set raw(lang: "java-new") #set par(leading: 0.75em) -#assignment(10)[ +#assignment(9)[ Write a program to show that `static` blocks get executed before any object creation and implement the use of static variable to count the number of objects. ] - +#v(-1em) #scos("StaticExecution") === Discussion diff --git a/java/text/StudentsArray.typ b/java/text/StudentsArray.typ new file mode 100644 index 0000000..18921ef --- /dev/null +++ b/java/text/StudentsArray.typ @@ -0,0 +1,38 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.6em) + +#assignment(11, block: true)[ + Write a program to make a student class with attributes for _roll number_, _name_ and _stream_. Assume that a student studies 3 subjects having full marks of 100 each. Each subject has a _title_ and _theory marks_. From the main class, create an array of such students. Show their specific information, along with average percentage of marks. +] + +#scos("StudentsArray", pad: 3em) + +=== Discussion + +#skind[Classes, and methods used from Java standard library] + +- `java.util.Scanner` class: + - `long nextLong()`: Scan a `long` value from the input stream. + - `String nextLine()`: Scan a line of text from the input stream. + - `int nextInt()`: Scan an `int` value from the input stream. + +#skind[Classes and methods implemented in the program] + +- The `Subject` class represents a subject with a title and theory marks. + - Constructor `Subject(String, int)`: Create a subject with the given title and theory marks. + - `static Subject input(Scanner)`: Takes input for subject details from the user. + - `int getTheoryMarks()`: Returns the theory marks for the subject. + - `void display()`: Display the marks obtained in the subject. + +- The `Student` class represents a student with a roll number, name, stream, and an array of subjects. + - Constructor `Student(long, String, String, Subject[])`: Create a student with the given details. + - `static Student input(Scanner)`: Takes input for student details from the user. + - `void display()`: Display the student details, including roll number, name, stream, subject details, and average percentage. + +- The `StudentsArray` class contains the main program to take input for multiple students and display their details: + - `public static void main(String[])`: Takes the number of students, inputs details for each student, and displays their details including average percentage. + + +#signature() diff --git a/java/text/Template.typ b/java/text/Template.typ new file mode 100644 index 0000000..d72ff21 --- /dev/null +++ b/java/text/Template.typ @@ -0,0 +1,20 @@ +#import "/template.typ": * +#show: A => apply(A) +#set raw(lang: "java-new") +#set par(leading: 0.7em) + +#assignment()[ + +] + +#scos("") + +=== Discussion + +#skind[Classes, interfaces and methods used from Java standard library] + + +#skind[Classes and methods implemented in the program] + + +#signature() |