









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 15
This page cannot be seen from the preview
Don't miss anything!










@Override public String toString() { return "Triangle: side1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3 + "\n" + super.toString(); } }
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(); } }
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(); } } }
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); } }
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); } }
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.
public interface Colorable { void howToColor(); }
public abstract class GeometricObject { public abstract double getArea(); }
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."); } }
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(); } }
public class Circle extends GeometricObject { private double radius;
public Circle(double radius) { this.radius = radius; }
public double getArea() { return Math.PI * radius * radius; } }