Java Programming Assignment: Geometric Objects and Exception Handling, Exams of Nursing

A java programming assignment focusing on object-oriented programming (oop) concepts and exception handling. It includes exercises on creating geometric objects, implementing inheritance, and handling exceptions for invalid triangle inputs. The assignment provides detailed methodologies, uml diagrams, code examples, and expected outputs, making it a valuable resource for students learning java programming and oop principles. It covers topics such as class creation, inheritance, polymorphism, and exception handling, offering practical experience in implementing these concepts in java.

Typology: Exams

2025/2026

Available from 12/18/2025

KingPrince
KingPrince 🇺🇸

887 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 3
COSC1047: INTRODUCTION TO COMPUTER SCIENCE 2
MAYURKUMAR MANIYA
249484080
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Programming Assignment: Geometric Objects and Exception Handling and more Exams Nursing in PDF only on Docsity!

ASSIGNMENT 3

COSC1047: INTRODUCTION TO COMPUTER SCIENCE 2

MAYURKUMAR MANIYA

EXERCISE: 1

METHODOLOGY:

  • Create a GeometricObject class with attributes for color and filled state.
  • Define a constructor in GeometricObject to initialize its attributes.
  • Create a Triangle class that extends GeometricObject and adds three sides.
  • Implement constructors in Triangle to initialize the side lengths, color, and filled status.
  • Implement the getArea() method using Heron’s Formula for area calculation.
  • Implement the getPerimeter() method to sum up the sides of the triangle.
  • Override the toString() method in Triangle to display triangle properties.
  • Develop a TriangleTest class to take user input and create a Triangle object.
  • Print the triangle details, including its color, fill state, area, and perimeter.
  • Run the program in Eclipse to verify correctness and validate user input.

UML DIAGRAM:

@Override public String toString() { return "Triangle: side1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3 + "\n" + super.toString(); } }

  • TriangleTest,java package assignment3;

import java.util.Scanner;

public class TriangleTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

System.out.print("Enter side1: "); double side1 = scanner.nextDouble(); System.out.print("Enter side2: "); double side2 = scanner.nextDouble(); System.out.print("Enter side3: "); double side3 = scanner.nextDouble(); scanner.nextLine();

System.out.print("Enter color: "); String color = scanner.nextLine();

System.out.print("Is the triangle filled? (true/false): "); boolean filled = scanner.nextBoolean();

Triangle triangle = new Triangle(side1, side2, side3); triangle.setColor(color); triangle.setFilled(filled);

System.out.println("\nTriangle Details:"); System.out.println(triangle); System.out.printf("Area: %.2f%n", triangle.getArea()); System.out.printf("Perimeter: %.2f%n", triangle.getPerimeter());

scanner.close(); } }

OUTPUT:

CONCLUSION:

This program uses OOP concepts like inheritance to create a Triangle class from GeometricObject. It calculates the area and perimeter using Heron’s formula and displays the details clearly. The code is simple, reusable, and well-structured, making it easy to understand and use.

package TriangleExceptionProject;

import java.util.Scanner;

public class TestTriangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

try { System.out.print("Enter side 1: "); double side1 = scanner.nextDouble(); System.out.print("Enter side 2: "); double side2 = scanner.nextDouble(); System.out.print("Enter side 3: "); double side3 = scanner.nextDouble();

Triangle triangle = new Triangle(side1, side2, side3); triangle.display(); } catch (IllegalTriangleException e) { System.out.println("Exception: " + e.getMessage()); } finally { scanner.close(); } } }

  • Triangle.java package TriangleExceptionProject;

public class Triangle { private double side1, side2, side3;

public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) { throw new IllegalTriangleException("Invalid Triangle"); } this.side1 = side1; this.side2 = side2; this.side3 = side3; }

public void display() { System.out.println("Valid Triangle: " + side1 + ", " + side2 + ", " + side3); } }

OUTPUT:

CONCLUSION:

The IllegalTriangleException ensures that only valid triangles are created by checking the sides against the triangle inequality theorem. The program handles invalid input by throwing an exception and displaying an error message. This solution demonstrates basic exception handling and data validation in a simple, effective way.

while (scanner.hasNext()) { if (scanner.hasNextDouble()) { double score = scanner.nextDouble(); scores.add(score); total += score; } else if (scanner.next().equalsIgnoreCase("done")) { break; } } scanner.close();

if (scores.isEmpty()) { System.out.println("No scores entered."); return; }

Collections.sort(scores); double average = total / scores.size();

System.out.println("Scores in Ascending Order: " + scores); System.out.println("Total: " + total); System.out.println("Average: " + average); } }

OUTPUT:

CONCLUSION:

This program lets users input scores, sorts them in ascending order, and calculates the total and average. It uses an ArrayList to store the data and Collections.sort() for sorting. The program handles user input and errors smoothly, demonstrating basic input handling, sorting, and calculations in Java.

CODE:

  • Colorable.java package colorableExample;

public interface Colorable { void howToColor(); }

  • GeometricObject.java package colorableExample;

public abstract class GeometricObject { public abstract double getArea(); }

  • Square.java package colorableExample;

public class Square extends GeometricObject implements Colorable { private double side;

public Square() { this.side = 0; // No-arg constructor, side = 0 }

public Square(double side) { this.side = side; }

public double getSide() { return side; }

public void setSide(double side) { this.side = side; }

@Override public double getArea() { return side * side; }

@Override public void howToColor() { System.out.println("Color all four sides."); } }

  • TestProgram.java package colorableExample;

import java.util.Scanner;

public class TestProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

GeometricObject[] objects = new GeometricObject[5];

for (int i = 0; i < 5; i++) { System.out.print("Enter side length for Square " + (i + 1) + ": "); while (!scanner.hasNextDouble()) { System.out.println("Invalid input! Please enter a valid number."); scanner.next(); } double side = scanner.nextDouble(); objects[i] = new Square(side); }

System.out.println("\nResults:"); for (GeometricObject obj : objects) { System.out.println("Area: " + obj.getArea()); if (obj instanceof Colorable) { ((Colorable) obj).howToColor(); } System.out.println("----------------"); }

scanner.close(); } }

  • Circle.java (Optional) package colorableExample;

public class Circle extends GeometricObject { private double radius;

public Circle(double radius) { this.radius = radius; }

public double getArea() { return Math.PI * radius * radius; } }

OUTPUT: