Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can I know why is my code showing this particular error during run. Here is the question: My codes : enum Type {Equilateral, Isoceles, Scalene,

Can I know why is my code showing this particular error during run.

image text in transcribed

Here is the question:

image text in transcribed

image text in transcribed

My codes :

enum Type {Equilateral, Isoceles, Scalene, NotTriangle}

//PossibleTriangle class attributes class PossibleTriangle{ private int a; private int b; private int c; private Type pt; public PossibleTriangle(int a, int b, int c, Type pt) { this.a = a; this.b = b; this.c = c; pt = getType(); } public PossibleTriangle(int i, int j, int k) { // TODO Auto-generated constructor stub } // Check if values is more than 0 for it to be a triangle private boolean isTriangle() { if ((a > 0 && b > 0 && c > 0) && ((a + b > c) && (b + c > a) && (a + c > b))) { return true; } else { return false; } } //Check if values make up a triangle and what type of Triangle private Type getType() { if (isTriangle()) { if(a==b && b==c & a==c) { return Type.Equilateral; } else if (a==b || b==c || a==c) { return Type.Isoceles; } else if (a!=b && b!=c && a!=c) { return Type.Scalene; } } return Type.NotTriangle; } //text to output in displayInfo public void displayInfo() { System.out.println("a = " + a + "," + " b = " + b + "," + " c = " + c); //Display according to type of triangle switch(pt) { case NotTriangle: System.out.println("Not a triangle"); break; case Scalene: System.out.println("A triangle"); break; case Equilateral: System.out.println("Equilateral triangle"); System.out.println("Isosceles triangle"); System.out.println("A triangle"); break; } } } class Lab_4{ public static void main(String[]args){ PossibleTriangle possibleTriangle1 = new PossibleTriangle(4, 5, 6); possibleTriangle1.displayInfo(); PossibleTriangle possibleTriangle2 = new PossibleTriangle(1, 2, 3); possibleTriangle2.displayInfo(); PossibleTriangle possibleTriangle3 = new PossibleTriangle(4, 5, 5); possibleTriangle3.displayInfo(); PossibleTriangle possibleTriangle4 = new PossibleTriangle(6, 6, 6); possibleTriangle4.displayInfo(); } }

run: a = 0, b = 0, c = 0 Exception in thread "main" java.lang.NullPointerException at PossibleTriangle.displayInfo 84.java:54) at Lab_4.main b4.java:75) C:\UsersRappData\Local NetBeans Cache\8.2\executor-snippets un.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) Lab 4 File Name: Your Name_Lab_4.java Given three integers a, b and c. These 3 integers can form the three sides of a triangle if and only if a, b and c is positive and the sum of any two integers is greater than the other integer. There are three types of triangles: (a) Isosceles triangle, any two sides which are equal (b) Equilateral triangle, all the three sides which are equal. Note that an equilateral triangle is also an isosceles triangle. (C) Scalene triangle, all the three sides are different. Possible Triangle -int a -intb - intc - Type pt > Type Equilateral Isosceles Scalene A NotTriangle 1..1 -pt + Possible Triangle(int a, int b, intc) -boolean is Triangle - Type getType +void displayinfo Lab_4 + static void main(Stringl args) In the UML diagram, you need to explore a few methods in the design: A method to test and to return true or false) if three integers can form a triangle A method to test and to return the Type of a triangle A method to display the triangle info An instance variable pt should be initialized it inside the constructor to an appropriate data type. The following features must be included in the design: (a) Using if-else statement to identify whether it is a triangle. If it is a triangle, what is its type? (b) Using switch-case statement to print out the triangle's information, i.e. information as shown in the above. In the main method (no reading), you construct four possible triangle objects; upon execution, you will get the following display: a = 4, b = 5, c = 6 A triangle a = 1, b = 2, c = 3 Not a triangle a = 4, b = 5, c = 5 Isosceles triangle A triangle a = 6, b = 6, C = 6 Equilateral triangle Isosceles triangle A triangle During the demo, you may be asked to change the values of a triangle. You must respect basic programming style in writing this program Indentations and alignment of statements / braces A blank line between methods/classes/tasks - Efficiently use of comment statements Avoid long statements Declarations

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions