Assignment 1 Documentation
Assignment 1 Documentation
Assignment 1
Group: A
Exercise 1: Chat Users
The task is to create a class named ChatUser with the following attributes:
Nickname
Age
Location
Each user has a unique nickname when the default constructor is used.
Constructed users display a message confirming their addition to the chat.
Encapsulation of attributes with private access.
To design the class I used private fields: nickname, age, location and a static counter to create
unique nicknames for each user. Also I used a status variable to tell if a user is in the chat or no.
Constructors:
No-parameter constructor: Assigns default values (Anonymous<number>, age = 20, location =
"U.S.A").
Single-parameter constructor: Accepts age, uses default nickname and location.
Two-parameter constructor: Accepts nickname and age, sets location to "U.S.A".
Three-parameter constructor: Fully custom initialization.
In order to create different default usernames at the no parameter constructor and single
parameter constructor I use a counter which increments every time a new user is created.
Exercise 2: Invoice
The task is to create an Invoice class that encapsulates the details of a customer invoice. The
class includes features to handle customer data, items purchased, taxation, discounts, and overall
invoice management.
Class Design
1. Invoice Class
Attributes:
o UUID id: Unique identifier for the invoice.
o LocalDate dateCreated: Date the invoice is issued.
o LocalDate dueDate: Defaults to 7 days after the issue date.
o Customer customer: Customer information.
o Item[] itemList: Array of up to 10 items.
o boolean isListEmpty: Tracks whether the item list is empty.
Methods:
o addItems(): Dynamically add items to the invoice (max 10).
o calcDiscount(): Applies a 10% discount if the total cost exceeds 5000 ALL.
o valueAddedTax(): Calculates VAT as 20% of the total cost.
o totalCost(): Computes the total price of all items before discounts or VAT.
o toalAfterDiscount(): Computes the total cost after discounts and VAT.
o toString(): Provides a detailed invoice summary.
2. Customer Class
Attributes:
o String name: Customer name.
o String email: Email address.
o String address: Physical address.
Methods:
o Default and parameterized constructors.
o Getters and setters for all attributes.
o toString(): Formats customer details for display.
3. Item Class
Attributes:
o int idNumber: Unique identifier for the item.
o String name: Item name.
o double price: Price per unit.
o int quantitySold: Quantity of the item sold.
Methods:
o Constructor to initialize item data.
o Getters and setters for all attributes.
o toString(): Formats item details for display.
Implementation Details
1. addItems():
o Prompts the user to add items dynamically.
o Ensures the list doesn’t exceed 10 items.
o Provides feedback if the list is full.
Example:
java
Copy code
invoice.addItems();
2. calcDiscount():
o Calculates a 10% discount for totals exceeding 5000 ALL.
o Uses array iteration to sum item costs.
3. valueAddedTax():
o Computes 20% VAT on the total item cost.
4. toString():
o Provides a detailed invoice summary, including:
Customer details.
List of items.
Total cost before and after applying VAT and discounts.
Exercise 3: Assembled computers
The objective is to design a system for managing a company's inventory of assembled computers. Each
computer's attributes and its components (CPU, RAM, Hard Disk, Motherboard) are modeled as
separate classes.
The requirements state that each Computer is composed of multiple components: CPU, RAM,
Hard Disk, and Motherboard. Each component is represented as a separate class with relevant
attributes and methods. Default constructors provide pre-configured objects, and parameterized
constructors allow for customization.
Class Design
1. Computer Class
Attributes:
o CPU cpu: Processor details.
o RAM ram: Memory details.
o HardDisk hardDisk: Storage details.
o Motherboard motherboard: Motherboard details.
o int creationYear: Year of assembly.
o double price: Price of the computer.
Key Methods:
o Default and parameterized constructors for flexible initialization.
o Getters and setters for all attributes.
o toString(): Combines details of all components into a formatted string.
2. CPU Class
Attributes:
o int clockFrequency: Frequency in GHz.
o SetArchitecture setArchitecture: CPU architecture (enum: CISC or RISC).
o int typeBits: Processor type (32 or 64 bit).
o int numberRegisters: Number of registers.
o Manufacturer manufacturer: Manufacturer (enum: AMD, INTEL).
Key Methods:
o Default and parameterized constructors for initialization.
o Getters and setters for modifying attributes.
o toString(): Displays CPU specifications in a readable format.
3. RAM Class
Attributes:
o int size: Size in GB.
o Type type: RAM type (enum: DDR, DDR2, DDR3).
o String manufacturer: Manufacturer name.
Key Methods:
o Default and parameterized constructors.
o Getters and setters.
o toString(): Formats RAM details for display.
4. HardDisk Class
Attributes:
o int size: Storage capacity in GB.
o int rotationsMinute: Speed in RPM.
o String manufacturer: Manufacturer name.
Key Methods:
o Default and parameterized constructors.
o Getters and setters.
o toString(): Outputs Hard Disk specifications.
5. Motherboard Class
Attributes:
o String chipsetVersion: Version of the chipset.
o int usbPorts: Number of USB ports.
o int ramCapacity: Maximum supported RAM in GB.
o String manufacturer: Manufacturer name.
Key Methods:
o Default and parameterized constructors.
o Getters and setters.
o toString(): Formats motherboard details for display.
Exercise 4: Estimation of constant e
To solve this exercise I imported the BigDecimal and BigInteger classes from java.math.
Then I designed a method to calculate the factorial since by definition:
1 1 1
e = 1+ + +…..+
1! 2! n!
Then by using a different method calculate e and with a different method print e.
Since the Fibonacci sequence uses the first term and second term to build the ongoing sequence I
use 3 variables of BigInteger to calculate the terms:
BigInteger first = BigInteger.ONE;
BigInteger second = BigInteger.ONE;
BigInteger current;
Then I determine the bounds since the requirements state that only numbers with n digits must be
printed . After using a Scanner object to get n, I determine the bounds as minimum: 10n−1 and
maximum: 10n . So if n=3 minimum is number is 100 (included) and maximum number is 1000
(not included). So by using a loop and a condition to check if the term is inbetween the minimum
and maximum print the term and generate a new term.
Exercise 6: DNA sequence operations
The exercise requires different operations:
1. Forming the complementary sequence
To create this method I use a object of StringBuilder and build a for each loop that
switches through cases (ex. Case: A append T) and also be careful for default
arguments . The method returns the complement of the initial String.