Decomposition + MVC

Decomposition

Problem: Write a Java program that parses a text file with commands for different types of shapes containing information about their size, coordinates, and position. The program should calculate the area of each shape.

Decompose the problem following Model-View-Controller practices.

File format: shape,dimension info, x, y, color

triangle,side:10,100,150,BLUE
square,side:15,20,15,YELLOW
circle,radius:12,50,60,GREEN

Decomposition – UML diagram

Draw a UML diagram based on your group discussion on how to decompose this problem

Your diagram should have a superclass, and subclasses for model. It should also have a controller class and a view class

Quiz 7

current time

You have 15 minutes.

UML diagram

Implementation – model

Equilateral triangle area = (side² × √3)/ 4

JUnit tests:

@Test
void testTriangleArea() {
  Shape triangle = new Triangle(4);
  Assertions.assertEquals(triangle.getArea(), 6.93);
}

@Test
void testSquareArea() {
  Shape square = new Square(4);
  Assertions.assertEquals(square.getArea(), 16);
}

@Test
void testCircleArea() {
  Shape circle = new Circle(4);
  Assertions.assertEquals(circle.getArea(), 50.27);
}

Implementation – view (ascii version)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class View {
    
    public static void main(String[] args) throws FileNotFoundException {
        Controller controller = new Controller();
        
        File file = new File(args[0]);
        Scanner reader = new Scanner(file);
        
        while (reader.hasNext()) {
            controller.parseLine(reader.nextLine());
        }
        
        System.out.println(controller);
        reader.close();
        
    }

}

Implementation – view (JavaFX version)

What GUI components do we need?

What GUI-specific methods should be created?