Commit 550d94d8 authored by cosmina.dunca's avatar cosmina.dunca

Commit

parents
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
File added
## Assessment Exercise 2 - Shapes
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Inheritance
4. Encapsulation
5. Abstraction
## 2 Shapes
The code in this project contains an abstract class `Shape` used to define the methods any shape class must possess (see the Inheritance section of the module website).
I've created a `Triangle` class as an example shape for you.
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written. See Assessment Exercise 1 for more details on Unit Testing.
## 4 Task
You need to create to classes which inherit (`extend`) from `Shape`. These are
* `Square`
* `Rectangle`
### `Square`
The `Square` class should store the information and methods related to a square shape.
1. `size` should be used to represent the width and height of the square.
2. `Square` should have two constructors. The first should have no parameters and set the size equal to 3. The second should take a single integer parameter and use it to set the size.
3. You should be able to use the `getSize()` and `setSize()` methods properly for the `Square`.
4. You should correctly implement the `area()` and `circumference()` methods for `Square`.
5. You should implement a `draw()` method that will output an ascii square of the correct size.
6. All the Square unit tests must be passed.
### `Rectangle`
The `Rectangle` class should store the information and methods related to a Rectangle shape.
1. Instead of using `size` you should use `width` and `hieght` to represent the width and height of the rectangle.
2. `Rectangle` should have two constructors. The first should have no parameters and set the `width` equal to 3 and the `height` equal to 4. The second should take a two integer parameter and uses them to set the `width` and `height` (in that order).
3. You should be able to use the `getWidth()` and `setWidth()` methods properly for the `Rectangle`.
4. You should be able to use the `getHeight()` and `setHeight()` methods properly for the `Rectangle`.
5. You should correctly implement the `area()` and `circumference()` methods for `Rectangle`.
6. You should implement a `draw()` method that will output an ascii rectangle of the correct size.
7. All the Rectangle unit tests must be passed.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
### `Square` (10 Marks)
* constructors - 2 marks
* setSize(), getSize() - 2 marks
* area(), circumference() - 2 marks
* draw() - 2 marks
* Pass all Square Unit Tests - 2 marks
### `Rectangle` (15 Marks)
* constructors - 3 marks
* setWidth(), getWidth(), setHeight(), getHeight() - 3 marks
* area(), circumference() - 3 marks
* draw() - 3 marks
* Pass all Square Unit Tests - 3 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12noon 16/05/22.
\ No newline at end of file
File added
File added
File added
public class App {
public static void main(String[] args) throws Exception {
// Triangle tests
// 1
/* Shape testShape = new Triangle();
System.out.println(testShape.getSize()); -> correct */
// 2
/* Shape testShape = new Triangle(5);
System.out.println(testShape.getSize()); -> correct */
// 3
/* Shape testShape = new Triangle(5);
testShape.setSize(6);
System.out.println(testShape.getSize()); -> correct */
// 4
/* Shape testShape = new Triangle(6);
System.out.println(testShape.circumference()); -> correct */
// 5
/* Shape testShape = new Triangle(6);
System.out.println(testShape.area()); -> correct */
// 6
/* Shape shape1 = new Triangle(5);
shape1.draw(); */
// Square tests
// 1
/* Shape testShape = new Square();
System.out.println(testShape.getSize()); -> correct */
// 2
/* Shape testShape = new Square(5);
System.out.println(testShape.getSize()); -> correct */
// 3
/* Shape testShape = new Square(5);
testShape.setSize(6);
System.out.println(testShape.getSize()); -> correct */
// 4
/* Shape testShape = new Square(6);
System.out.println(testShape.circumference()); -> correct */
// 5
/* Shape testShape = new Square(5);
System.out.println(testShape.area()); -> correct */
// 6
/* Shape shape1 = new Square(5);
shape1.draw(); -> good */
// Rectangle tests
// 1
/* Shape testShape = new Rectangle();
System.out.println(testShape.getWidth());
System.out.println(testShape.getHeight()); -> correct */
// 2
/* Shape testShape = new Rectangle(5,6);
System.out.println(testShape.getWidth());
System.out.println(testShape.getHeight()); -> correct */
// 3
/* Shape testShape = new Rectangle(5,4);
testShape.setWidth(6);
System.out.println(testShape.getWidth()); -> correct */
/* Shape testShape = new Rectangle(7, 8);
testShape.setHeight(3);
System.out.println(testShape.getHeight()); -> correct */
// 4
/* Shape testShape = new Rectangle(6,3);
System.out.println(testShape.circumference()); */
// 5
/* Shape testShape = new Rectangle(5,7);
System.out.println(testShape.area()); -> correct */
// 6
/* Shape shape1 = new Rectangle(20, 12);
shape1.draw(); -> good */
}
}
## Assessment Exercise 2 - Shapes
## 1 Skills
This exercise should help you with:
1. Member variables and member methods.
2. Unit testing.
3. Inheritance
4. Encapsulation
5. Abstraction
## 2 Shapes
The code in this project contains an abstract class `Shape` used to define the methods any shape class must possess (see the Inheritance section of the module website).
I've created a `Triangle` class as an example shape for you.
## 3 Unit Testing
This project is set up with unit testing enabled. Unit testing is a development technique where tests can be created in advance and used to help ensure the code is corrent when it is written. See Assessment Exercise 1 for more details on Unit Testing.
## 4 Task
You need to create to classes which inherit (`extend`) from `Shape`. These are
* `Square`
* `Rectangle`
### `Square`
The `Square` class should store the information and methods related to a square shape.
1. `size` should be used to represent the width and height of the square.
2. `Square` should have two constructors. The first should have no parameters and set the size equal to 3. The second should take a single integer parameter and use it to set the size.
3. You should be able to use the `getSize()` and `setSize()` methods properly for the `Square`.
4. You should correctly implement the `area()` and `circumference()` methods for `Square`.
5. You should implement a `draw()` method that will output an ascii square of the correct size.
6. All the Square unit tests must be passed.
### `Rectangle`
The `Rectangle` class should store the information and methods related to a Rectangle shape.
1. Instead of using `size` you should use `width` and `hieght` to represent the width and height of the rectangle.
2. `Rectangle` should have two constructors. The first should have no parameters and set the `width` equal to 3 and the `height` equal to 4. The second should take a two integer parameter and uses them to set the `width` and `height` (in that order).
3. You should be able to use the `getWidth()` and `setWidth()` methods properly for the `Rectangle`.
4. You should be able to use the `getHeight()` and `setHeight()` methods properly for the `Rectangle`.
5. You should correctly implement the `area()` and `circumference()` methods for `Rectangle`.
6. You should implement a `draw()` method that will output an ascii rectangle of the correct size.
7. All the Rectangle unit tests must be passed.
## 5 Marking
25 marks are available for this exercise. This breaks down as follows.
### `Square` (10 Marks)
* constructors - 2 marks
* setSize(), getSize() - 2 marks
* area(), circumference() - 2 marks
* draw() - 2 marks
* Pass all Square Unit Tests - 2 marks
### `Rectangle` (15 Marks)
* constructors - 3 marks
* setWidth(), getWidth(), setHeight(), getHeight() - 3 marks
* area(), circumference() - 3 marks
* draw() - 3 marks
* Pass all Square Unit Tests - 3 marks
## 6 Submission
Your project should be uploaded to your gitlab account and you should submit a link to the gitlab project, alongside the rest of the assessment, through moodle, by 12noon 16/05/22.
\ No newline at end of file
public class Rectangle extends Shape {
// A rectangle's width and height are different
protected int width;
protected int height;
public Rectangle() {
this.width = 3;
this.height = 4;
}
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
// getters
public int getWidth() { return width; }
public int getHeight() { return height; }
// setters
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
@Override
public void draw() {
for(int i = 1; i <= height; ++i){
for(int j = 1; j <= width * 2 + 2; ++j) {
if (j == 1 || j == 2 * width + 2){
System.out.print('|');
} else if (i == 1 || i == height) {
System.out.print('-');
} else {
System.out.print(' ');
}
}
System.out.println();
}
}
@Override
public float area() {
float area = width * height;
return area;
}
@Override
public float circumference() {
float circ = 2 * width + 2 * height;
return circ;
}
}
File added
abstract public class Shape {
protected int size;
abstract public void draw();
abstract public float area();
abstract public float circumference();
abstract public int getWidth();
abstract public int getHeight();
abstract void setWidth(int width);
abstract void setHeight(int height);
public int getSize() { return size; }
public void setSize(int size) { this.size = size; }
public Shape(){
this.size = 3;
} // Constructor without parameter
public Shape(int size){
this.size = size;
} // Constructor with a parameter
}
public class Square extends Shape {
// A square's width and height are the same
public Square() {
super();
}
public Square(int i) {
super(i);
}
@Override
public void draw() {
for(int i = 1; i <= size; ++i) {
for(int j = 1; j <= size * 2 + 1; ++j) {
if(j == 1 || j == size * 2 + 1) {
System.out.print('|');
} else if (i == 1 || i == size) {
System.out.print('-');
} else {
System.out.print(' ');
}
}
System.out.println();
}
}
@Override
public float area(){
float fSize = size;
float area = fSize * fSize;
return area;
}
@Override
public float circumference() {
float fSize = size;
float circ = fSize * 4;
return circ;
}
}
public class Triangle extends Shape{
// An equilateral triangle
public Triangle() {
super();
}
public Triangle(int i) {
super(i);
}
@Override
public void draw() {
for (int loop1=0; loop1<size; loop1++){
for (int loop2=loop1; loop2<(size-1); loop2++) {
System.out.print(" ");
}
System.out.print("/");
for (int loop2=0; loop2<loop1; loop2++){
System.out.print(" ");
}
System.out.println("\\");
}
for (int loop1=0; loop1<size; loop1++){
System.out.print("--");
}
System.out.println("");
}
@Override
public float area() {
float fSize = size;
float area = 0.5f * fSize * fSize;
return area;
}
@Override
public float circumference() {
float fSize = size;
float circ = fSize + fSize +fSize;
return circ;
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class unitTest {
// *********************************
// Triangle Tests
// *********************************
@Test
public void testTriangleConstructor() {
// test default constructor
Shape testShape = new Triangle();
assertEquals(3, testShape.getSize());
// test constructor with size parameter
testShape = new Triangle(5);
assertEquals(5, testShape.getSize());
}
@Test
public void testTriangleSize() {
// test setSize()
Shape testShape = new Triangle(5);
testShape.setSize(6);
assertEquals(6, testShape.getSize());
}
@Test
public void testTriangleCircumference() {
// test circumference()
Shape testShape = new Triangle(6);
assertEquals(18.0, testShape.circumference(), 1e-6);
}
@Test
public void testTriangleArea() {
// test area()
Shape testShape = new Triangle(6);
assertEquals(18.0, testShape.area(), 1e-6);
}
// *********************************
// Square Tests
// *********************************
@Test
public void testSquareConstructor() {
// test default constructor
Shape testShape = new Square();
assertEquals(3, testShape.getSize());
// test constructor with size parameter
testShape = new Square(5);
assertEquals(5, testShape.getSize());
}
@Test
public void testSquareSize() {
// test setSize()
Shape testShape = new Square(5);
testShape.setSize(6);
assertEquals(6, testShape.getSize());
}
@Test
public void testSquareCircumference() {
// test circumference()
Shape testShape = new Square(6);
assertEquals(24.0, testShape.circumference(), 1e-6);
}
@Test
public void testSquareArea() {
// test area()
Shape testShape = new Square(5);
assertEquals(25.0, testShape.area(), 1e-6);
}
// *********************************
// Square Tests
// *********************************
@Test
public void testRectangleConstructor() {
// test default constructor
Shape testShape = new Rectangle();
assertEquals(3, testShape.getWidth());
assertEquals(4, testShape.getHeight());
// test constructor with size parameter
testShape = new Rectangle(5,6);
assertEquals(5, testShape.getWidth());
assertEquals(6, testShape.getHeight());
}
@Test
public void testRectangleWidth() {
// test setSize()
Shape testShape = new Rectangle(5,4);
testShape.setWidth(6);
assertEquals(6, testShape.getWidth());
}
@Test
public void testRectangleHeight() {
// test setSize()
Shape testShape = new Rectangle(78);
testShape.setHeight(3);
assertEquals(3, testShape.getHeight());
}
@Test
public void testRectangleCircumference() {
// test circumference()
Shape testShape = new Rectangle(6,3);
assertEquals(18.0, testShape.circumference(), 1e-6);
}
@Test
public void testRectangleArea() {
// test area()
Shape testShape = new Rectangle(5,7);
assertEquals(35.0, testShape.area(), 1e-6);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment