Function isn't calculating correctly? - java

After creating three objects it is supposed to take the radius and height from the user, calculate the volume and then ask again for the next cylinder. When I run this code it does prompt me for the radius and height, but it does not calculate the volume. What am I doing wrong?
import javax.swing.JOptionPane;
import java.io.*;
public class CylinderTest
{
public static void main(String[] args) throws IOException
{
String input;
String input2;
double radius[] = new double[3];
double height[] = new double[3];
Cylinder[] myCylinder = new Cylinder[3];
for (int i = 0; i < myCylinder.length; i++)
{
input = JOptionPane.showInputDialog("Enter a radius");
radius[ i ] = Double.parseDouble(input);
input2 = JOptionPane.showInputDialog("Enter a height" +
"");
height[i] = Double.parseDouble(input2);
myCylinder[i].height = input2;
myCylinder[i].radius = input;
JOptionPane.showMessageDialog(null, " The volume of the cylinder is: " + myCylinder.getVolume());
}
}
static class Cylinder
{
private double radius;
private double height;
public Cylinder(double radius, double height)
{ this.radius = radius;
this.height = height;
}
public double getVolume()
{
return radius * radius * height * Math.PI;
}
}
}

You are using the keyword static for your variables and methods inside your Cylinder class. I'm not sure why you did use the static keyword, but static implies there will be only one instance for the whole program (i.e. it is a global variable or function) . To fix it, delete the word static in the Cylinder class's methods and member variables. You may also want to look at Math.PI instead of using 3.1416 as PI.
Also, thought I'd mention, that you don't need to store the volume in a variable. Instead, you can simply return it. Unless of course you want to cache it in a variable for some reason, but seeing as you're recalculating it every call to getVolume(), there is no need to store the volume in a variable.
i.e.
public double getVolume() // notice: no static keyword in the method's prototype
{
return (radius * radius) * height * Math.PI;
}
Pro tip: Don't use magic numbers in your code, use a variable to store the magic number. As a constant (final) variable or a regular one.
e.g.
static final int MEANING_OF_LIFE = 42; // this is somewhere tucked in a class or method
// static is used because the final keyword
// is used and therefore isn't modified
// Meaning that there is no real point to have
// multiple MEANING_OF_LIFE variables, hence static
// ...
System.out.printf("The meaning of life is: %i", MEANING_OF_LIFE);

You forgot
myCylinder[i].height = input2;
myCylinder[i].radius = input;
Plus,
JOptionPane.showMessageDialog(null, " The volume of the cylinder is: " + myCylinder.getVolume());

remove the static from getVolume(). remove the static from fields volume, height.
To calculate you have to create a cylinder object, and get the volume:
Cylinder cyl = new Cylinder(radius, height);
double volume = cyl.getVolume();

Related

add object directly in a array

I have written a object for a school assignment. now i'm trying to add it to an array so i can add multiple boxes.
my main
String name;
double userInputLength;
double userInputWidth;
double userInputHeight;
// initialise a scanner to be able to read the user input
Scanner reader = new Scanner(System.in);
// ask the user for input
System.out.print ("Enter the name of your box: ");
// read this input
name = (reader.nextLine());
// ask the user for input
System.out.print ("Enter the length of your box: ");
// read this input
userInputLength = (reader.nextDouble());
// ask the user for input
System.out.print ("Enter the width of your box: ");
// read this input
userInputWidth = (reader.nextDouble());
// ask the user for input
System.out.print ("Enter the height of your box: ");
// read this input
userInputHeight = (reader.nextDouble());
Block blockOne = new Block(name, userInputLength, userInputWidth, userInputHeight);
System.out.println( blockOne.showBoxAsString());
My object
package model;
import java.util.ArrayList;
import java.util.List;
public class Block {
// name variable of the figure
private String name;
// dimension variable of the figure
private double blockWidth;
private double blockHeight;
private double blockLength;
public Block() {
}
// form a block
public Block(String N, double L, double W, double H){
this.name = N;
this.blockLength = L;
this.blockWidth = W;
this.blockHeight = H;
}
// set the name
public void setBlockName(String N){ this.name = N; }
// set the name
public String getBlockName(){ return this.name; }
//set length method
public void setLength(double L)
{
this.blockLength = L;
}
//get length method
public double getLenght(){
return this.blockLength;
}
//set width method
public void setWidth(double W)
{
this.blockLength = W;
}
//get width method
public double getWidth(){
return this.blockWidth;
}
//set height method
public void setHeight(double H)
{
this.blockLength = H;
}
//get height method
public double getHeight(){
return this.blockHeight;
}
// method to calculate the surface of the shape (in this situation its a box)
public double calcSurfaceBox(){
// the formula to calculate the surface of the box is length times the width
double surface = 2 * (this.blockHeight * this.blockWidth) +
2 * (this.blockLength * this.blockHeight) +
2 * (this.blockWidth * this.blockLength) ;
// return the calculated value of surface
return surface;
}
// method to calculate the volume of the shape (in this situation it's a box)
public double calcVolumeBox(){
// the formula to calculate the volume is length times width times height
double volume = this.blockLength * this.blockWidth * this.blockHeight;
// return the calculated value of volume
return volume;
}
// a method to print a string to show the user the size of the shape (in this case a box.)
public String showBoxAsString(){
return String.format( "The box has a name: " + getBlockName() + "\n" +
"The box has a Length of: " + getLenght() + "\n" +
"The box has a Width of: " + getWidth() + "\n" +
"The height of the box is: " + getHeight());
}
}
i searched for multiple solutions but i can't figure it out. Is there anyone that could give me some tools or an idea?
my goal is to make my main code as clean as possible. so if anyone got an idea how i can simplify my main code that would be awesome.
Initialise an array of Block Objects
Block blocks[] = new Block[length];
To access each object use blocks[index]
Note: The array elements here store the reference variables to the object
Edit: index here means a block object perhaps blockOne

java returning and calling variables

I am new to java and I am trying to make this bmi calculator but I am having trouble returning and calling variables. I am sure that I am doing something very wrong but have been unable to figure out how to properly do this after searching the internet my guess is I do not know what I should be searching. I will post the code, I am getting 4 errors in my main that are as follows:
required: double,double,double,double
found: no arguments
reason: actual and formal argument lists differ in length
I am assuming that I have improperly set up my variables but could really use a bit of guidance. Thank you in advance.
import java.util.Scanner;
public class cs210 {
public double weight;
public double height;
public double bmi;
public double wcal;
public double mcal;
public double age;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
method1 ();
method2 ();
method3 ();
method4 ();
method5 ();
}
public static void method1 () {
System.out.println ("This program implements a Health Assistance Calculator ");
System.out.println ("Given a weight, height, and age, it will compute:\n");
System.out.println ("BMI - Body Mass Index");
System.out.println ("Calories needed per day to maintain weight");
}
public double method2 (double weight, double height, double wcal, double bmi) {
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter your weight:");
weight = keyboard.nextDouble ();
System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
double wunits = keyboard.nextDouble();
if (wunits == 1) {
System.out.println("Thank you");
} else if (wunits == 2){
weight = weight / 2.2;
System.out.println("Thank you");
}
else {
System.out.println ("Please try again");
return 0;
}
System.out.println("Please enter your height:");
height = keyboard.nextDouble ();
System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
int hunits = keyboard.nextInt();
if(hunits ==1) {
System.out.println("Thank you");
} else if (hunits == 2){
height = height / 0.0254;
}else {
System.out.println("Please try again");
return 0;
}
System.out.println("Please enter your age in years:");
age = keyboard.nextDouble ();
bmi = weight / Math.pow(height, height);
return ( bmi + age + height + weight);
}
public static double method3(double weight, double age, double height) {
double paf = 1.375;
double mcal;
mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
return mcal;
}
public static double method4(double weight, double age, double height, double paf){
double wcal;
wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
return wcal;
}
public double method5(double bmi, double mcal, double wcal){
System.out.println("Your BMI is:" + bmi);
System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
System.out.println("To maintain your current weight:");
System.out.println("Men need" + mcal + "per day");
System.out.println("Women need" + wcal + "per day");
return 0;
}
}
You define method2 like this:
public double method2 (double weight, double height, double wcal, double bmi) {
// ...
It has four parameters, all double, just like your error message said. Then you call it like this:
method2 ();
Without any parameters at all, again just like the error message said. Since you defined it with four parameters, every time you call it you need to do it with four parameters. The values you use as parameters will be the values that the variables weight, height, wcal and bmi gets inside the function, and if you don't have any parameters the computer will not know what values to use for those variables and therefore throw an error to complain. So you could, as an example, do it like this:
method2(34.9, 23.4, 23.5, 34.1); // Just picked four random numbers here.
But looking at the structure of your program, it looks like you don
t want to pass any values to the function at all (since you let the user enter the values inside the function). Then you could just get rid of the parameters, and declare the variables inside the function:
public double method2 () {
double weight, height, wcal, bmi;
// ...
Now the variables will be available inside method2, but not anywhere else. If you want to use the same values later in the other functions, you could instead of declaring them inside your function declare them in your class, and they will become available anywhere in your class, but not anywhere else.
You will have to fix the same issue with the parameters for method3, method4 and method5 as well.
You need to pass parameters when you call to methods. If you call to method
public double method2 (double weight, double height, double wcal, double bmi)
You need to call it to like this method2 (50, 2, 200, 25.5);
When you call in to your other methods such as method3, method4, method5 ; you have to give appropriate parameters to those. But when it comes to your method1. It will not expecting any parameters so you don't want to pass any parameter to that method.
I think this small document will help you to understand method and arguments.
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
Its better to add your BMI logic and method to separate class then within the main method create and object and to the rest of manipulation. Otherwise it will hard to maintain and update properties and when you do it in that way remove your static methods. and use proper names for each and every method.
This code will give compilation errors because you have called methods in wrong way and you have call method2 and method5 within static method.
in method2() you have given 4 parameter but you are not using even a single parameter because you are getting from user.
like your modified code is
import java.util.Scanner;
public class cs21`enter code here`0 {
public static double weight;
public static double height;
public static double bmi;
public double wcal;
public double mcal;
public static double age;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
cs210 cs=new cs210();
method1 ();
double val=cs.method2 ();
double value1=cs.method3 (weight,age,height);
double value2=cs.method4 (weight,age,height);
cs.method5 (bmi,value1,value2);
}
public static void method1 () {
System.out.println ("This program implements a Health Assistance Calculator ");
System.out.println ("Given a weight, height, and age, it will compute:\n");
System.out.println ("BMI - Body Mass Index");
System.out.println ("Calories needed per day to maintain weight");
}
public double method2 () {
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter your weight:");
weight = keyboard.nextDouble ();
System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
double wunits = keyboard.nextDouble();
if (wunits == 1) {
System.out.println("Thank you");
} else if (wunits == 2){
weight = weight / 2.2;
System.out.println("Thank you");
}
else {
System.out.println ("Please try again");
return 0;
}
System.out.println("Please enter your height:");
height = keyboard.nextDouble ();
System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
int hunits = keyboard.nextInt();
if(hunits ==1) {
System.out.println("Thank you");
} else if (hunits == 2){
height = height / 0.0254;
}else {
System.out.println("Please try again");
return 0;
}
System.out.println("Please enter your age in years:");
age = keyboard.nextDouble ();
bmi = weight / Math.pow(height, height);
return ( bmi + age + height + weight);
}
public static double method3(double weight, double age, double height) {
double paf = 1.375;
double mcal;
mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
return mcal;
}
public static double method4(double weight, double age, double height){
double wcal;
double paf=1.375;
wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
return wcal;
}
public void method5(double bmi, double mcal, double wcal){
System.out.println("Your BMI is:" + bmi);
System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
System.out.println("To maintain your current weight:");
System.out.println("Men need" + mcal + "per day");
System.out.println("Women need" + wcal + "per day");
}
}
Actually to make your code work the way it is written you should:
make all the fields and methods static
remove parameters from all methods declarations
declare local variable paf in method4.
That being said the code in that form is quite ugly. You should think about the following improvements:
class name should start from capital letter
class name should be something meaningful (e.g. BcmCalculator)
fields should be private
method should have meaningful names ( printGreetings, readUsersAttributes, etc)
in main method you should create instance of the class and call its methods
paf should be a constant (ie field private static final double PAF = 1.375;).
There are further possible improvements, but this should be enough for the beginning.

non-static variable cannot be referenced from a static context in println statement

I know that these types of questions get asked a lot and I've read
this one
and
this one
but for some reason I'm still having troubles understand the issue my current program is having.
I'm trying to create a set of classes that defines a series of 3-D shapes that can store its size, and provides access to change the data. It should also be able to calculate circumference, area, and volume. I've only gotten to the point where I'm doing my first chosen shape (a sphere) and I'm getting this error in the println inside the if statement.
import java.util.Scanner;
public class Shapes
{
public static void main(String[] args)
{
String input;
double Radius;
Scanner scan = new Scanner(System.in);
System.out.println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
input = scan.next();
if (input.equals("S"))
{
System.out.println("Enter radius: ");
Radius = scan.nextDouble();
Sphere calcCircumference;
Sphere calcArea;
Sphere calcVolume;
System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");
}
}
}
public class Sphere
{
// instance variables
protected double radius;
protected double circumference;
protected double area;
protected double volume;
Scanner scan = new Scanner(System.in);
/**
* Constructor for objects of class Sphere
*/
public Sphere()
{
// initialise instance variables
radius = 0;
circumference = 0;
area = 0;
volume = 0;
}
/**
*Gets user entered radius
*/
public double getRadius(double Radius)
{
radius = radius;
return radius;
}
public double calcCircumference()
{
circumference = 2*Math.PI*radius;
return circumference;
}
public double calcArea()
{
area = 4*Math.PI*Math.pow(radius,2);
return area;
}
public double calcVolume()
{
volume = (4*Math.PI*Math.pow(radius,3))/3;
return volume;
}
}
Any help or guidance would be appreciated.
System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");
You need to create an instance of Sphere before you can access it fields.
Sphere sphere = new Sphere();
You then need to provide the information that the object needs
sphere.radius = Radius; // I'd prefer a setter method
Then you can make use of the information that this instance provides...
System.out.println("The circumference is "+sphere.calcCircumference()+", the area is "+Sphere.calcArea()+", the volume is "+Sphere.calcVolume()+".");
There is no need to keep track of circumference, area or volume, they are calculated fields, you simply need to call the methods you need to get the calculated result.
The fields circumference, area, volume and radius are known as instance fields, they require an instance of the class before you can use them. This means you can have multiple instances of Sphere each with there own unquie values
You might like to take a closer look at Classes and Objects
I changed your code a bit, now it is working (may not be the most efficient way):
import java.util.Scanner;
public class Shapes {
public static void main(String[] args) {
String input;
double Radius;
Scanner scan = new Scanner(System.in);
System.out
.println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
input = scan.next();
if (input.equals("S")) {
System.out.println("Enter radius: ");
Radius = scan.nextDouble();
Sphere calcCircumference;
Sphere calcArea;
Sphere calcVolume;
System.out.println("The circumference is " + Sphere.circumference
+ ", the area is " + Sphere.area + ", the volume is "
+ Sphere.volume + ".");
}
}
public static class Sphere {
// instance variables
protected double radius;
protected static double circumference;
protected static double area;
protected static double volume;
Scanner scan = new Scanner(System.in);
/**
* Constructor for objects of class Sphere
*/
public Sphere() {
// initialise instance variables
radius = 0;
circumference = 0;
area = 0;
volume = 0;
}
/**
* Gets user entered radius
*/
public double getRadius(double Radius) {
radius = radius;
return radius;
}
public double calcCircumference() {
circumference = 2 * Math.PI * radius;
return circumference;
}
public double calcArea() {
area = 4 * Math.PI * Math.pow(radius, 2);
return area;
}
public double calcVolume() {
volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
return volume;
}
}
}

Why doesn't the return of my method work?

I have this assignment for a Java class I'm taking in college. I'm supposed to write a program that gets the length and width from a user and display the perimeter of a rectangle. Then, it needs to return the area. I have written the source code for it, but my IDE, Eclipse keeps saying that I'm using a voided method, therefore, it can't return.
Here is the source code:
/** Jason Delgado
* SPC ID: 2051577
* This program gets the length and width of a rectangle from the user, displays its perimeter,
* and returns it area.
*/
package com.delgado;
import javax.swing.JOptionPane; //Gets the JOptionPane class
import java.text.DecimalFormat; //Gets the DecimalFormat class
public class Rectangle {
public static void main(String[] args) {
double length; // Holds length
double width; // Holds width
String input; // Holds input
double area; // Holds area
DecimalFormat formatter = new DecimalFormat("#0.00"); // Holds format
input = JOptionPane.showInputDialog("Enter the length of the rectangle: ");
length = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter the width of the rectangle: ");
width = Double.parseDouble(input);
area = getArea(length, width);
/** The method getArea() displays the perimeter of a rectangle and returns the area
* #perm num1 Holds the length
* #perm num2 Holds the width
* #return Returns the area of a rectangle
*/
public static double getArea(double num1, double num2){
double perimeter; // Holds perimeter
perimeter = (num1 * 2) + (num2 * 2);
JOptionPane.showMessageDialog(null, "The perimeter of the rectangle is: " + formatter.format(perimeter));
return num1 * num2;
}
}
}
EDIT Since you've added your code, I have edited my answer:
It looks like your getArea method is inside your main. Move it outside.
public class Rectangle {
public static void main(String[] args) {
....
}
public static double getArea(double num1, double num2) {
...
}
}
syntax for a method :
access_modifier return_type method_name(parameters) {
...
return value_under_that_return_type;
}
place your methods in this order:
... getArea() {...}
... main() {...}
You shouldn't be nesting functions.

Calculating perimeter and area of a rectangle

I need to be able to input length and width of a rectangle into a console and calculate its perimeter and area. I have it working other than accepting my inputs for the calculations. I know I'm close, but can't seem to figure it out. Thanks in advance for your help. Keep in mind I'm a novice to put it nicely, so your answers may not make sense to me at first. I cannot get it to calculate the values that I input into the console.
package edu.purdue.cnit325_lab1;
public class Rectangle {
private static double length;
private static double width;
public Rectangle() {
length=0.0;
width=0.0;
}
public Rectangle(double l, double w) {
length = l;
width = w;
}
public double FindArea() {
return length*width;
}
public double FindPerim() {
return length*2 + width*2;
}
}
package edu.purdue.cnit325_lab1;
import java.util.Scanner;
public class TestRectangle {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanL = new Scanner (System.in);
System.out.print("Please enter the length of the rectangle: ");
double L = scanL.nextDouble();
Scanner scanW = new Scanner (System.in);
System.out.print("Please enter the length of the rectangle: ");
double W = scanW.nextDouble();
//int W = scanW.nextInt();
double RectangleArea;
Rectangle unitRectangle = new Rectangle();
RectangleArea = unitRectangle.FindArea();
System.out.println("The area of a unit rectangle is " + RectangleArea);
double RectanglePermiter;
Rectangle perimRectangle = new Rectangle();
RectanglePermiter = perimRectangle.FindPerim();
System.out.println("The permimiter of the unit rectangle is " + RectanglePermiter);
}
}
Note that you are calling the Rectangle constructore with no arguments thus setting its width and height to zero, you should use
Rectangle unitRectangle = new Rectangle(L,W);
and indeed like the other answer you should use one Scanner instance.
Plus regarding coding style: do not upercase your variable names. Its quite confusing for more "experienced" java developers. :-)
you missed to call parameterized constructor.
public static void main(String[] args) {
Scanner scanL = new Scanner (System.in);
System.out.print("Please enter the length of the rectangle: ");
double L = scanL.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double W = scanL.nextDouble();
Rectangle rectangle = new Rectangle(l,w);
double rectangleArea = rectangle .FindArea();
System.out.println("The area of a unit rectangle is " + rectangleArea);
double rectanglePermiter = rectangle.FindPerim();
System.out.println("The permimiter of the unit rectangle is " + rectanglePermiter);
}
Note: Unnecessarily you created two Scanner objects and two Rectangle objects in your code,which are removed from the above code.
Use one Scanner instance. Just reuse it.
Scanner scanner = new Scanner (System.in);
System.out.print("Please enter the length of the rectangle: ");
double L = scanner.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double W = scanner.nextDouble();
Update: You don't pass the L and W to the constructor as the other answer points out. However, some mistakes you made:
You declared length and width as static. Don't do that. That makes no sense. The length and the width are properties of a rectangle and shouldn't be shared by all rectangle instances.
You don't use the correct naming conventions: variables start with a lowercase character, class names start with an uppercase character.
You are creating two instances of your Rectangle to calculate both perimeter and area of the same rectangle. Share that instance instead.
So you need to set the values in some way... you can do either
A)
Rectangle unitRectangle = new Rectangle(l,w);
B)
or create getters and setters in the rectangle class..
setLength(double l) length = l;
setWidth(double w) width = w
double getLength() return length;
double getWidth() return height;
Since you are initializing with the default constructor
aka
Rectangle unitRectangle = new Rectangle();
the values for length and width will also be zero.
Your code consists of the Default constructor, which will initialize the respective values length and width to 0.0 as set by you and also consist of parametrized constructor which requires input values to be provided and will set the value accordingly.
When you are creating the object of your class, you are calling the Default Constructor instead of parametrized constructor in this line
Rectangle unitRectangle = new Rectangle();
Thus setting them to 0.0
If you do Something like this
Rectangle unitRectangle2 = new Rectangle(2.3,4.3);
This will create the object having values for length and breadth as 2.3 and 4.3 respectively.
//write a java program which will calculate area and perimeter of rectangle by accepting radius from cmd prompt
//area = width x height,perimeter = (2 x width) + (2 x height)
class AreaAndPerOfRect
{
int h,w;
void set(int x,int y)
{
h=x;
w=y;
}
int AreaOfRect()
{
int area=w*h;
return area;
}
int PerOfRect()
{
int per=(2*w)+(2*h);
return per;
}
void disp()
{
int area=AreaOfRect();
System.out.println("area of rectangle"+area);
int per=PerOfRect();
System.out.println("area of rectangle"+per);
}
}
class AreaAndPerOfRectDemo
{
public static void main(String args[])
{
if(args.length!=2)
{
System.out.println("please enter two values");
}
else
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
AreaAndPerOfRect ap=new AreaAndPerOfRect();
ap.set(x,y);
ap.disp();
}
}
}
//This is the Java code for finding the area and perimeter of a rectangle
package demo;
import java.util.Scanner;
public class DemoTranslation {
public static int area(int length, int width) {
int areaOfRectangle;
areaOfRectangle = length * width;
System.out.println("Area of Rectangle is : " + areaOfRectangle);
return 0;
}
public static int perimeter(int length, int width) {
int perimeterOfRectangle;
perimeterOfRectangle = (length + width) * 2;
System.out.println("Perimeter of Rectangle is : " + perimeterOfRectangle);
return 0;
}
public static void main(String[] args) {
int length, width, choice;
System.out.println("Enter the length of the triangle ");
length = STDIN_SCANNER.nextInt();
System.out.println("Enter the width of the triangle ");
width = STDIN_SCANNER.nextInt();
System.out.println("Enter 1 : View the area ");
System.out.println("Enter 2 : View the perimeter ");
System.out.println("Enter 3 : view both ");
choice = STDIN_SCANNER.nextInt();
switch(choice) {
case 1:
area(length, width);
break;
case 2:
perimeter(length, width);
break;
case 3:
area(length, width);
perimeter(length, width);
break;
default:
System.out.println("Invalid option ");
break;
}
}
public final static Scanner STDIN_SCANNER = new Scanner(System.in);
}

Categories

Resources