Java return value not as expected - java

I am new Java programmer. I am writing simple program to calculate area of the rectangle.You get to input the width and height of the rectangle, but issue is whatever values I enter, the area value is always returning zero. How can i fix this issue. Please have look at my cde.
import java.util.Scanner;
public class Shape {
private int area;
private int width;
private int length;
private String name;
public String shapeName() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter shape name: ");
String name = scanner.nextLine();
return name;
}
public int area() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
String width = scanner.nextLine();
System.out.print("Enter height: ");
String height = scanner.nextLine();
return this.width * this.length;
}
}
public class Example1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape shape = new Shape();
System.out.println("Shape is " + shape.shapeName());
System.out.println("It's area is " + shape.area());
}
}
Thanks... have a great day! :)

Your problem is that you are not assigning the input to your class variables:
private int width;
private int length;
But to your method local variables String width and String length.
So the line return this.width * this.length; will return 0 because both this.width and this.length weren't changed so they are 0 because in Java int is by default initialized to 0.
You should assign the input to your class variables.
public int area() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
width = scanner.nextInt();
System.out.print("Enter height: ");
length = scanner.nextInt();
return width * length;
}
Note:
Use Scanner.nextInt() to get int values instead of Scanner.nextLine() which will return a String, otherwise you should parse these Strings back to int.

String width = scanner.nextLine();
System.out.print("Enter height: ");
String height = scanner.nextLine();
return this.width * this.length;
}
Here you are putting the width in two locals variables, and the return the mul of the property width and height, that are different variables. Try with
this.width = scanner.nextInt();
System.out.print("Enter height: ");
this.height = scanner.nextInt();
return this.width * this.length;

This should fix your problem bro!
public int area() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
int width = Integer.parseInt(scanner.nextLine());
this.width = width;
System.out.print("Enter height: ");
int height = Integer.parseInt(scanner.nextLine());
this.height = height;
return this.width * this.length;
}

Assigning value to local new variable means,
this.width and this.length is not used and hence returning 0
public int area() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
this.width = scanner.nextInt();
System.out.print("Enter height: ");
this.height = scanner.nextInt();
return this.width * this.length;
}

Do something like this in the class Shape.
You need to initialize global variables width and length using this keyword
class Shape {
private int area;
private int width;
private int length;
private String name;
public String shapeName() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter shape name: ");
String name = scanner.nextLine();
return name;
}
public int area() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
int width = scanner.nextInt();
System.out.print("Enter lengtht: ");
int length = scanner.nextInt();
this.width = width;
this.length=length;
return this.width * this.length;
}
}

Use this code also
public double area(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter width: ");
int width = scanner.nextInt();
System.out.print("Enter height: ");
int height = scanner.nextInt();
double d=width*height;
return d;
}

Related

Allowing user to enter non standard characters such as " or ' in JAVA

Ok, So I don't know what I'm missing and I've gone through my material and googled for hours. What I'm trying to do is allow a user to input height and inches using integers and the respective " and '.
I appreciate your help with this:
int height, weight;
double bmi;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is your height in ft/in. You can say for" +
" example 5'9\" :" );
height = keyboard.nextInt();
System.out.print("What is your weight in lb :");
weight = keyboard.nextInt();
bmi = weight *703 / (height*height);
System.out.println("Your bmi is : " + bmi);
You will need to create your own Height class like:
class Height {
private int feet;
private int inches;
public Height(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
public void setfeet(int feet)
{
this.feet = feet;
}
public void setinches(int inches)
{
this.inches= inches;
}
public int getfeet()
{
return this.feet;
}
public int getinches()
{
return this.inches;
}
}
Then in your main class, you can do like:
System.out.print("What is your height -> feet: );
feet = keyboard.nextInt();
System.out.print("What is your height -> inches: );
inches= keyboard.nextInt();
Height height = new Height(feet,inches);

How to implement a double type method

Implement a method that is passed by two double type numbers, prints out their total and doesn't return anything. Guys how does this look now???
import java.util.Scanner;
public class Tutorial1 {
public static void main1(double num1, double num2)
{
double numb1=3.5;
double numb2=3.4;
double total = numb1 + numb2;
Scanner reader = new Scanner (System.in);
System.out.println(total);
numb1 = reader.nextInt();
numb2 = reader.nextInt();
}
I didn't understand what u want well try this hope helps to u
import java.util.Scanner;
public class MyFirstJavaProgram {
public static void main(String []args) {
main1(3.65,2.85);
}
public static double main1 (double numb12, double numb21){
int numb1 = 0, numb2 = 0;
int total = numb1 + numb2;
Scanner reader = new Scanner(System.in);
System.out.println("Enter two integers: ");
numb1 = reader.nextInt();
numb2 = reader.nextInt();
System.out.println(total+numb1+numb2);
return total+numb1+numb2;
}
}

Arguments within methods

I'm starting on methods today & I ran into an error that one of my equations in the main method isn't applicable throughout the whole program. It's the fourth line of code & I thought it was typed correctly. Basically, when you input the length & width, you will get the area of a rectangle as the output. Here's the code:
double area = areaOfRectangle();
String YES = "Y";
String YES2 = "y";
String NO = "N";
String NO2 = "n";
boolean validInput = false;
System.out.print("Please enter a length: ");
float length = input.nextInt();
System.out.print("Please enter a width: ");
float width = input.nextInt();
System.out.printf("Area: %.2d %n", area);
do{
System.out.print("Enter more? (Y/N): ");
String input2 = input.next();
if(input.hasNextLine()){
if(input2.equals("Y") || input2.equals("y")){
System.out.print("Please enter a length: ");
length = input.nextFloat();
System.out.print("Please enter a width: ");
width = input.nextFloat();
System.out.printf("Area: %.2d %n", area);
}
else if(input2.equals("N") || input2.equals("n")){
System.exit(1);
}
}
}while(!validInput);
}
public static void areaOfRectangle(float length2, float width2){
length2 = length;
width2 = width;
double rectangle = (length2 * width2);
}
}
What am I doing wrong?
You have
double area = areaOfRectangle();
and
public static void areaOfRectangle(float length2, float width2){
So you have a method returning void (nothing), which compiles since you're not returning anything, but you're trying to assign that nothing to a double. Also, you're not passing the parameters at all. In java, you cannot assign a method to a variable as in a functional language.
You need:
public static double areaOfRectangle(float length2, float width2){
double rectangle = (double)length2 * width2;
return rectangle;
}
And then calculate area when you have the parameters:
double area = areaOfRectangle(length,width);
Maybe don't do conversions between float and double either, just use doubles throughout.

Java function returning zero as a result

First off sorry about the title I could not think how I should title this. The prompt of the assignment had us calculate the volume and surface area of a pyramid and prism with three different classes(two classes with constructors and one test class), and as the title states I keep getting zero.
Here is the prism class:
public class Prism
{
double l;
double w;
double h;
public Prism(double intL, double intW, double intH)
{
double l = intL;
double w = intW;
double h = intH;
}
public double getPrismVolume()
{
return l*w*h;
}
public double getPrismSurfaceArea()
{
return 2*((l*w)+(h*l)+(h*w));
}
}
Here is my Pyramid class:
public class Pyramid
{
double b;
double h;
public Pyramid(double intB, double intH)
{
double b = intB;
double h = intH;
}
public double getPyramidVolume()
{
return (1.0/3.0)*Math.pow(b,2)*h;
}
public double getPyramidSurfaceArea()
{
return Math.pow(b,2)+(2*b*h);
}
}
Here is my test class:
import java.util.*;
public class GeometryTest
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
System.out.print("Enter the length of the prism: ");
String answer1 = myScanner.nextLine();
double length = Double.parseDouble(answer1);
System.out.print("Enter the width of the prism: ");
String answer2 = myScanner.nextLine();
double width = Double.parseDouble(answer2);
System.out.print("Enter the height of the prism: ");
String answer3 = myScanner.nextLine();
double height = Double.parseDouble(answer3);
System.out.print("Enter the pyramid's base: ");
String answer4 = myScanner.nextLine();
double base = Double.parseDouble(answer4);
System.out.print("Enter the pyramid's height: ");
String answer5 = myScanner.nextLine();
double pyramidHeight = Double.parseDouble(answer5);
Pyramid aPyramid = new Pyramid(base,pyramidHeight);
Prism aPrism = new Prism(length,width,height);
System.out.println("The prism's volume is: " + aPrism.getPrismVolume());
System.out.println("The prism's surface area is: " + aPrism.getPrismSurfaceArea());
System.out.println("The pyramid's volume is: " + aPyramid.getPyramidVolume());
System.out.println("The pyramid's surface area is: " + aPyramid.getPyramidSurfaceArea());
}
}
Your constructors are declaring local variables and ignoring the instance variables. The instance variables are left uninitialized, so Java initializes them to their default value, 0.
E.g. change
double l = intL;
to
l = intL;
so l will resolve to the instance variable name.
You are hiding your class level fields with variable shadows;
public Prism(double intL, double intW, double intH)
{
// double l = intL;
// double w = intW;
// double h = intH;
this.l = intL;
this.w = intW;
this.h = intH;
}
and the same problem in Pyramid -
public Pyramid(double intB, double intH)
{
// double b = intB;
// double h = intH;
this.b = intB;
this.h = intH;
}

I'm trying to call a double variable from one method into another

How do i call the length and width variable into the getArea method without creating a private variable in the class, the way I'm doing it is causing the method to run again after its already ran once. I really don't like it this way but thats the way the professor wants it done to simulate the times before "object oriented programming"
/*This program allows the user to enter the length and widtch and receive the area
of the rectangle*/
import java.util.Scanner;
public class theRectangleCompany
{
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getLength();
getWidth();
getArea();
}
public static double getLength()
{
double length;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the length ");
length = keyboard.nextDouble();
return length;
}
public static double getWidth()
{
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the width ");
width = keyboard.nextDouble();
return width;
}
public static void getArea()
{
double length = getLength();
double width = getWidth();
double area = width * length;
System.out.println("The area of the Rectangle is: " +area);
}
}
Why are you calling getLength() and getWidth() from the main method. Just call getArea()
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
You could make the getArea function take parameters, and use the function calls to the other two functions as the parameters:
getArea(getLength(), getWidth());
public static void getArea(double length, double width) { ... }
changes are here:
/*This program allows the user to enter the length and widtch and receive the area
of the rectangle*/
import java.util.Scanner;
public class theRectangleCompany
{
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
public static double getLength()
{
double length;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the length ");
length = keyboard.nextDouble();
return length;
}
public static double getWidth()
{
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the width ");
width = keyboard.nextDouble();
return width;
}
public static void getArea()
{
double length = getLength();
double width = getWidth();
double area = (width * length);
System.out.println("The area of the Rectangle is: " +area);
}
}
Not sure if this is what you want:
public static void getArea()
{
System.out.println("The area of the Rectangle is: " + (getLength() * getWidth()));
}
You'd also need to change the main method to exclude the getLength() and getWidth():
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
An variant to the above is something like
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea(getLength(),getWidth());
}
public static void getArea(double length, double width)
{
System.out.println("The area of the Rectangle is: " + (length * width));
}

Categories