Skip to document

Excercise 1 practice basicsyntax

Basic syntax practice
Course

Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Academic year: 2023/2024
Uploaded by:
0followers
3Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

####### Assignment 2

Basic Syntax - Introduction to Objects and Java Assignment Overview The aim of this assignment is to introduce you to Java and object-oriented programming concepts. You will build, test, and debug simple programs that create and manipulate compositions of objects. This will help you transition from Python to Java by leveraging your existing knowledge in a new context. The exercises are designed to be straightforward, akin to Python problems you may have encountered in CSC1015F. However, the shift to object-oriented programming requires a richer representation of concepts through the use of classes for objects, rather than basic data types like Strings or integers.

Exercise One [50 Marks]

This exercise involves the use of Rectangle and Area classes of objects described as follows: Class Rectangle A Rectangle object represents a geometric rectangle defined by its width and height. Constructors ● Rectangle(double width, double height) Creates a Rectangle object with the specified width and height. Methods ● public Area calculateArea() Calculates and returns the area of the rectangle as an Area object. ● public String toString() Returns a string representation of this Rectangle object in the form "Width: w, Height: h". Class Area An Area object represents the area of a geometric shape. Methods ● public double getValue() Returns the numeric value of the area.

● public String toString() Returns a string representation of this Area object in the form "Area: a". Assignment Instructions You will often use this style of describing object types in the course. A specification typically includes: ● The name of the class (type) and a brief description of what the objects represent. ● A description of constructors, the ways of creating instances of the type of object. ● A description of methods, the ways of manipulating instances of the type of object. For the Rectangle class, we've listed one way of creating it: a Rectangle object can be created by specifying its width and height. The following code snippet provides an example: Rectangle r; r = new Rectangle(5, 10); The code consists of two statements. The first declares a variable called r that will refer to a Rectangle object. The second uses an object creation expression and assigns the result to the variable. (Variable declaration and assignment can actually all be done in one statement, and that is what you’ll typically see in code samples in future.) We’ve given two methods for manipulating a Rectangle object. One is called calculateArea, which provides a means for calculating the area of the rectangle. The other method, toString, provides a means of obtaining a string representation of the object. Say we had a Rectangle object referred to as r, we could calculate its area using the expression r(). The calculateArea method returns a value that is an Area object. We’ve given you two methods for Area objects: getValue and toString. The getValue method returns the numeric value of the area. The toString method returns a string representation of the Area object. Now for your task: On the Amathuba page for the assignment, you will find Rectangle and Area classes. Download these and use them to write a Java program called CalculateRectangleArea that accepts as input the width and height of a rectangle, calculates the area of the rectangle, and then prints the result. Sample IO (The input from the user is shown in bold font – do not program this): Enter width: 5.

Area

public class Area { private double value; // Constructor public Area(double value) { this = value; } // Method to get the numeric value of the area public double getValue() { return this; } // Method to return a string representation of the Area public String toString() { return "Area: " + this; } }

CalculateRectangleArea

import java.util; public class CalculateRectangleArea { public static void main(String[] args) { Scanner scanner = new Scanner(System); // Prompt user for width System.out("Enter width:"); double width = scanner(); // Prompt user for height System.out("Enter height:"); double height = scanner(); // Create a Rectangle object Rectangle rectangle = new Rectangle(width, height); // Calculate the area Area area = rectangle(); // Print the result

System.out("The area of the rectangle with width " + width + " and height " +

height + " is " + area() + ".");

}

}

Exercise Two [50 Marks]

This exercise involves the use of Product and Store objects.

Class Product

A Product object represents an item in a store, with a name and a price.

Constructors

● Product(String name, double price)

Creates a Product object that represents the given item with the specified name

and price.

Methods

● public String getName()

Returns the name of the product.

● public double getPrice()

Returns the price of the product.

● public String toString()

Returns a string representation of the Product object in the form "Product: name,

Price: price".

Class Store

A Store object represents a collection of products and allows for adding products and

calculating the total cost.

Methods

● public void addProduct(Product product)

Adds the given product to the store's collection.

● public double calculateTotalCost()

Calculates and returns the total cost of all products in the store.

● public String toString()

Returns a string representation of all products in the store.

Assignment Instructions

You will often use this style of describing object types in the course. A specification typically

includes:

Enter product name or '[D]one' to finish: D Total cost: 2. Your program must make use of all the constructors and methods specified. Notes/Hints ● To determine whether one string, s1, is equal to another, s2, use the equals method, e., s1(s2). ● The charAt method accepts an index value as a parameter and, when applied to a string, returns the character at that position, e., the expression "CSC1016S".charAt(3) produces '1'. Example Program Below is an example implementation of the Product and Store classes, as well as the StoreManager program.

Product

public class Product { private String name; private double price; // Constructor public Product(String name, double price) { this = name; this = price; } // Getter for name public String getName() { return name; } // Getter for price public double getPrice() { return price; } // Method to return a string representation of the Product

public String toString() { return "Product: " + name + ", Price: " + price; } }

Store

import java.util; public class Store { private ArrayList<Product> products; // Constructor public Store() { products = new ArrayList<>(); } // Method to add a product to the store public void addProduct(Product product) { products(product); } // Method to calculate the total cost of all products in the store public double calculateTotalCost() { double totalCost = 0; for (Product product : products) { totalCost += product(); } return totalCost; } // Method to return a string representation of all products in the store public String toString() { StringBuilder result = new StringBuilder(); for (Product product : products) { result(product()).append("\n"); } return result();

System.out("Total cost: " + store()); } }

Was this document helpful?

Excercise 1 practice basicsyntax

Course: Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Was this document helpful?
Assignment 2
Basic Syntax - Introduction to Objects and Java
Assignment Overview
The aim of this assignment is to introduce you to Java and object-oriented programming
concepts. You will build, test, and debug simple programs that create and manipulate
compositions of objects. This will help you transition from Python to Java by leveraging your
existing knowledge in a new context. The exercises are designed to be straightforward, akin
to Python problems you may have encountered in CSC1015F. However, the shift to
object-oriented programming requires a richer representation of concepts through the use of
classes for objects, rather than basic data types like Strings or integers.
Exercise One [50 Marks]
This exercise involves the use of Rectangle and Area classes of objects described as
follows:
Class Rectangle
ARectangle object represents a geometric rectangle defined by its width and height.
Constructors
Rectangle(double width, double height)
Creates a Rectangle object with the specified width and height.
Methods
public Area calculateArea()
Calculates and returns the area of the rectangle as an Area object.
public String toString()
Returns a string representation of this Rectangle object in the form "Width: w,
Height: h".
Class Area
An Area object represents the area of a geometric shape.
Methods
public double getValue()
Returns the numeric value of the area.