Java help class and method errors code: /* * To change this license header, choo
ID: 665902 • Letter: J
Question
Java help
class and method errors
code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package circle;
import java.text.NumberFormat;
import java.util.Scanner;
//circle class w/ constructors
public class Circle {
/**
* @param args the command line arguments
*/
//main method
public static void main(String[] args) {
//prompt user
System.out.println("Welcome to the Circle Tester ");
Scanner sc = new Scanner(System.in);
}
public class CircleApp{
double radius, area, circumference;
String choice = ("y");
while (choice.equalsIgnoreCase("y"))
//get user input
{
System.out.print("Enter radius: ");
radius = sc.nextDouble();
}
}
public double getArea(){
area = Math.PI * (radius * radius);
return area;
}
public String getFormattedArea(){
return formatNumber(getArea());
}
public double getCircumference(){
circumference = Math.PI * 2*radius;
return circumference;
}
public String getFormattedCircumference(){
return formatNumber(getCircumference());
}
//format to decimal places
private string formatNumber(double x){
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(2);
String format = number.format(x);
return format;
System.out.println("Circumference: " + circumference);
System.out.println("Area: " + area);
}
}
public class Validator{
//ask user if they want to contine
System.out.println();
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine();
while (!choice.equalsIgnoreCase("y")
&& !choice.equalsIgnoreCase("n")) {
if (choice.equals("")) {
System.out .println("Error! This entry is required. Try again.");
System.out.print("Continue? (y/n): ");
} else {
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
System.out.print("Continue? (y/n): ");
}
choice = sc.nextLine();
}
}
}
MUST MEET THE FOLLOWING
Specifications
Create a class named Circle to store the data about this circle. This class should contain these constructors and methods:
public Circle(double radius)
public double getCircumference()
public String getFormattedCircumference()
public double getArea()
public String getFormattedArea()
private String formatNumber(double x)
public static int getObjectCount()
The formulas for calculating circumference and area are:
circumference = 2 * pi * radius
area = pi * radius2
For the value of pi, use the PI constant of the java.lang.Math class (Math.PI )
Create a class named CircleApp that gets the user input, creates a Circle object, and displays the circumference and area.
Create a class named Validator use its static methods to validate the data in this application
Explanation / Answer
CircleApp.java
package circle;
import java.util.Scanner;
/**
*
*/
public class CircleApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Circle Tester Do you want to calculate circumference and Area for Circle(y/n) ? ");
String userChoice = sc.nextLine();
String choice = ("y");
double radius;
while (choice.equalsIgnoreCase(userChoice)) {
System.out.print("Enter radius: ");
String userInput = sc.next();
// Validate the user input.validDouble is a static method.
if (Validator.validDouble(userInput)) {
radius = Double.parseDouble(userInput);
// Creating object for Circle
Circle circle = new Circle(radius);
System.out.println("Circle " + Circle.count + ":");
System.out.println("Radius: " + radius + " Area: " + circle.getFormattedArea() + " Circumference: " + circle.getFormattedCircumference());
} else {
System.out.println("Sorry given input is wrong.");
}
//Asking user want to continue
System.out.println("Do you want to continue(y/n) ? ");
userChoice = sc.next();
}
// User dont want to continue.
System.out.println("Thank you.");
}
}
Circle.java
package circle;
/**
*
*/
public class Circle {
double area;
double radius;
double circumference;
public static int count;
//Constructor
public Circle(double radius) {
this.radius = radius;
count++;
}
//Increment the object count
public static int getObjectCount() {
return count;
}
//Calculating the circle Circumference
public double getCircumference() {
circumference = 2 * Math.PI * radius;
return circumference;
}
public String getFormattedCircumference() {
return formatNumber(getCircumference());
}
//Calculating the circle Area
public double getArea() {
area= Math.PI * radius * 2;
return area;
}
public String getFormattedArea() {
return formatNumber(getArea());
}
// Converting Double value to String
private String formatNumber(double x) {
String number = Double.toString(x);
return number;
}
}
Validator.java
package circle;
import java.util.regex.Pattern;
/**
*
*/
public class Validator {
//Validating the user input is double or integer.
public static boolean validDouble(String userInput) {
try {
//Regular expression Pattern for validate Decimal
String decimalPattern = "([0-9]*)\.([0-9]*)";
//Regular expression Pattern for Integer
String regex = "[0-9]+";
boolean matchDouble = Pattern.matches(decimalPattern, userInput);
boolean match = Pattern.matches(regex, userInput);
//If given value is integer or Double return true, else false
if (match || matchDouble) {
return true;
} else {
return false;
}
} catch (NumberFormatException numberFormatException) {
System.out.println("Invalid input " + numberFormatException);
return false;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.