Area of Triangle, Java - java

I'm trying to build an app which computes the area of a triangle, as per my homework assignment. Not quite sure where I'm going wrong, but I input the lengths of the triangle and would like the proper area displayed according to Heron's formula: sqrt (s(s-a) (s-b) (s-c)). All I'm getting for output is -0.0. Here is the code:
import java.lang.Math;
public class Formula
{
double area; double s;
public double findArea(double sideA, double sideB, double sideC)
{
s = 1/2 * (sideA + sideB + sideC);
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
System.out.println("The area of the triangle is " + area);
return area;
}
}
And then I have another file for the main args
import java.util.Scanner;
public class findTriangleArea {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Formula triangle = new Formula();
double a,b,c;
// input triangle lengths a, b, c
Scanner inputTriangle = new Scanner(System.in);
System.out.println("Please enter triangle side a");
a = inputTriangle.nextDouble();
System.out.println("Please enter triangle side b");
b = inputTriangle.nextDouble();
System.out.println("Please enter triangle side c");
c = inputTriangle.nextDouble();
triangle.findArea(a, b, c);
}
}

1/2 is being computed in integer arithmetic, so like with all integer division, it's truncated -- in this case, to 0. Just write 0.5 and you'll be fine.

public class AreaOfTriangle {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the height: ");
double height=scanner.nextDouble();
System.out.print("Enter the base: ");
double base=scanner.nextDouble();
scanner.close();
double area=(base*height)/2;
System.out.println("---------------------------");
System.out.println("Area of Triangle is: "+area);
}
}

Heron's Formula:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a, b, c;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
double p = (a + b + c) / 2;
System.out.println(Math.sqrt(p * (p - a) * (p - b) * (p - c)));
}

Related

Returm to a specific line or place

I'm self learning Java for a few weeks and started testing my (basic) knowledge. I was trying to create something like some formula calculator, with an index selector. I want to know if it's possible to, after using a formula, go back to the first question. I tried to use a while loop, but I couldn't figure it out.
public class formulas {
public static void main(String[] args) {
// TODO Auto-generated method stub
// first question
// constant values definition
final double PI = 3.141592653589793238;
//index
ArrayList<String> index = new ArrayList<>();
index.add("1. Pythagorean Theorem");
index.add("2. Square Area");
index.add("2. Triangle Area");
System.out.println("Formulas Index:");
System.out.println("");
for(int i=0; i<index.size(); i++) {
System.out.println(index.get(i));
}
System.out.println("");
System.out.print("Write the formulas number you want to use: ");
Scanner scanner = new Scanner(System.in);
String firstQuestion = scanner.nextLine();
System.out.println();
// methods selection
if (firstQuestion.equals("1")) {
System.out.println("Pythagoras Theorem");
System.out.print("Insert side a: ");
double a = scanner.nextDouble();
System.out.print("Insert side b: ");
double b = scanner.nextDouble();
double c = pitagoras(a,b);
System.out.println("Hypotenuse c: "+ c);
}
else if (firstQuestion.equals("2")) {
System.out.println("Square area");
System.out.print("Insert side a: ");
double arestaA = scanner.nextDouble();
System.out.print("Insert side b: ");
double arestaB = scanner.nextDouble();
double squareArea = squarearea(arestaA,arestaB);
System.out.println("The square area is: "+ squareArea);
}
else System.out.println("Please insert a valid formula number.");
}
// methods parameters
static double pitagoras(double a, double b) {
double c = Math.sqrt((a*a)+(b*b));
return c;
}
static double squarearea(double a, double b) {
double area = a*b;
return area;
}
}
I'm always surprised that people don't make more use of do-while loops, it's a severely underrated construct.
Think about it, you MUST do at least one iteration of the loop before you know if you want to continue or exit the loop. You also want to re-print the menu on each iteration, so it's easier to just put it in a do-while (IMHO)
You can take a look at Control Flow Statements and The while and do-while Statements for more details
import java.util.ArrayList;
import java.util.Scanner;
public final class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
// first question
// constant values definition
final double PI = 3.141592653589793238;
//index
ArrayList<String> index = new ArrayList<>();
index.add("1. Pythagorean Theorem");
index.add("2. Square Area");
index.add("3. Triangle Area");
index.add("4. Quit");
Scanner scanner = new Scanner(System.in);
boolean printMenu = true;
boolean exit = false;
do {
if (printMenu) {
System.out.println("");
System.out.println("Formulas Index:");
System.out.println("");
for (int i = 0; i < index.size(); i++) {
System.out.println(index.get(i));
}
System.out.println("");
System.out.print("Write the formulas number you want to use: ");
}
printMenu = true;
String firstQuestion = scanner.nextLine();
System.out.println();
// methods selection
if (firstQuestion.equals("1")) {
System.out.println("Pythagoras Theorem");
System.out.print("Insert side a: ");
double a = scanner.nextDouble();
System.out.print("Insert side b: ");
double b = scanner.nextDouble();
double c = pitagoras(a, b);
System.out.println("Hypotenuse c: " + c);
} else if (firstQuestion.equals("2")) {
System.out.println("Square area");
System.out.print("Insert side a: ");
double arestaA = scanner.nextDouble();
System.out.print("Insert side b: ");
double arestaB = scanner.nextDouble();
double squareArea = squarearea(arestaA, arestaB);
System.out.println("The square area is: " + squareArea);
} else if (firstQuestion.equals("3")) {
// Triangle area
} else if (firstQuestion.equals("4")) {
exit = true;
} else {
printMenu = false;
System.out.println("Please insert a valid formula number.");
}
} while (!exit);
}
// methods parameters
static double pitagoras(double a, double b) {
double c = Math.sqrt((a * a) + (b * b));
return c;
}
static double squarearea(double a, double b) {
double area = a * b;
return area;
}
}
You can simply wrap your code starting from firstQuestion variable declaration till the end of the if condition in a while loop which is always set to true.
public class Formulas {
public static void main(String[] args) {
// first question
// constant values definition
final double PI = 3.141592653589793238;
// index
ArrayList<String> index = new ArrayList<>();
index.add("1. Pythagorean Theorem");
index.add("2. Square Area");
index.add("2. Triangle Area");
System.out.println("Formulas Index:");
System.out.println("");
for (int i = 0; i < index.size(); i++) {
System.out.println(index.get(i));
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("");
System.out.print("Write the formulas number you want to use: ");
String firstQuestion = scanner.nextLine();
System.out.println();
// methods selection
if (firstQuestion.equals("1")) {
System.out.println("Pythagoras Theorem");
System.out.print("Insert side a: ");
double a = scanner.nextDouble();
System.out.print("Insert side b: ");
double b = scanner.nextDouble();
double c = pitagoras(a, b);
System.out.println("Hypotenuse c: " + c);
} else if (firstQuestion.equals("2")) {
System.out.println("Square area");
System.out.print("Insert side a: ");
double arestaA = scanner.nextDouble();
System.out.print("Insert side b: ");
double arestaB = scanner.nextDouble();
double squareArea = squarearea(arestaA, arestaB);
System.out.println("The square area is: " + squareArea);
} else
System.out.println("Please insert a valid formula number.");
}
}
// methods parameters
static double pitagoras(double a, double b) {
double c = Math.sqrt((a * a) + (b * b));
return c;
}
static double squarearea(double a, double b) {
double area = a * b;
return area;
}
}

Java code has no errors, however compiler runs first class but not the proceeding classes

I tried to run this program in Netbeans, but it would only output the area of the rectangle. I was trying to get it to run in sequence after the input and output of rectangle. Was waiting for it to prompt the input for the square but it just stops at the rectangle. Any Suggestions?
import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
class SquareAreaDemo {
public void main extends Thread (String[] args)
{
System.out.println("Enter Side of Square:");
//Capture the user's input
Scanner scanner = new Scanner(System.in);
//Storing the captured value in a variable
double side = scanner.nextDouble();
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
class AreaTriangleDemo {
public void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
//Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}

How to get JOptionPane to calculate area?

I have created this code, the GUI pops up and works perfectly, but the area is not being calculated correctly. Any Clue why? I am very new to Java coding, so any help is appreciated. Thanks in advance.
package pkg4.pkg2.pkgnew.project;
import javax.swing.JOptionPane;
public class NewProject {
public static void main(String[] args) {
String inputStr = JOptionPane.showInputDialog("Type 1 for the area of Triangle, 2 for area of Circle, 3 for Rectangle, and 0 for area of none of these.");
int i = Integer.parseInt(inputStr);
if (i == 1) {
String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
int n1 = Integer.parseInt(inputStr);
String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
int n2 = Integer.parseInt(inputStr);
areaTriangle(n1, n2);
}
if (i == 2) {
String inpu = JOptionPane.showInputDialog("Enter a value to calculate the area of a circle: ");
double radius = Integer.parseInt(inputStr);
areaCircle(radius);
}
if (i == 3) {
String inp = JOptionPane.showInputDialog("Enter the first value to calculate the area of a rectangle: ");
int m1 = Integer.parseInt(inputStr);
String inp2 = JOptionPane.showInputDialog("Enter the second value to calculate the area of a rectangle: ");
int m2 = Integer.parseInt(inputStr);
areaRectangle(m1, m2);
} else {
return;
}
}
public static void areaTriangle(int n1, int n2) {
int areat = (n1 * n2) / 2;
JOptionPane.showMessageDialog(null, "The area of a triangle with your values is: " + areat);
}
public static void areaCircle(double radius) {
double areac = Math.PI * (radius * radius);
JOptionPane.showMessageDialog(null, "The area of a circle with your value is: " + areac);
}
public static void areaRectangle(int m1, int m2) {
int arear = (m1 * m2);
JOptionPane.showMessageDialog(null, "The area of a rectangle with your values is: " + arear);
}
public static void calcArea(int x) {
}
}
The problem with your code is that every time you parse the input into a string you are always using the same value of the string. Everytime you call your functions you are using all 1, 2, or 3 for your parameters into your area function calls. So you need to change the Integer.parseInt() to contain the new strings you get from the user like so:
String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
int n1 = Integer.parseInt(input); //not inputStr <----------
String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
int n2 = Integer.parseInt(inp);//not inputStr <---------
areaTriangle(n1, n2);

Error in Pythagorean Theorem Program

I am making a Pythagorean theorem program to solve for a missing side, and if the user enters 0 as the value that means that that is the missing side to solve for. My program is not getting the correct answer. Your help is greatly appreciated.
import java.util.Scanner;
import java.lang.Math;
public class pythagTheorem {
static double a;
static double b;
static double c;
static double newa;
static double newb;
static double newc;
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter the value of a");
a=scan.nextDouble();
scan.nextLine();
System.out.println("Enter the value of b");
b=scan.nextDouble();
scan.nextLine();
System.out.println("Enter the value of c");
c=scan.nextDouble();
scan.nextLine();
if(a==0)
{
newb=Math.pow(b, b);
newc=Math.pow(c, c);
double result=newc-newb;
newa=Math.sqrt(result);
}
System.out.println("The value of a is " + newa);
}
}
Okay! So the problem I see with this Java code would be that it doesn't necessarily follow the actual pythagorean theorem. I see that if (A==0) then execute the actual equation, but you must remember that variable A isn't the hypotenuse of the triangle!
A^2 + B^2 = C^2 (C being the hypotenuse) If you want to make it so that it finds any part of the triangle you must remember to derive the equation so that you can find the missing side! Such as, you have side A and C but you want to find side B, the equation would then be B^2 = C^2-A^2.
Last of all, I see you have newb=Math.pow(b, b); that would power your variable to itself! Even the the powers are only 2, so it would be newb=Math.pow(b, 2);
TIP! Remember to capitalize the second word in a variable name like numa to numA
/* REMEMBER THAT A NUMBER MUST NEVER BE GREAT THAN C UNLESS IT IS THE UNKNOWN OR YOU WILL ENCOUNTER A NONREAL NUMBER BECAUSE YOU CAN'T SQUARE ROOT A NEGATIVE NUMBER*/
import java.util.Scanner;
import java.lang.Math;
public class pythagTheorem {
static double a;
static double b;
static double c;
static double newa;
static double newb;
static double newc;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a ZERO for the unknown side!");
System.out.println("Enter the value of side A :: ");
a = scan.nextDouble();
System.out.println("Enter the value of B :: ");
b = scan.nextDouble();
System.out.println("Enter the value of C :: ");
c = scan.nextDouble();
if (a == 0) {
newc = Math.pow(c, 2);
newb = Math.pow(b, 2);
newa = Math.sqrt(newc - newb);
System.out.println("Your missing side is :: " + newa);
}
else if(b == 0){
newc = Math.pow(c, 2);
newa = Math.pow(a, 2);
newb = Math.sqrt(newc - newa);
System.out.println("Your missing side is :: " + newb);
}
else if(c == 0){
newa = Math.pow(a, 2);
newb = Math.pow(b, 2);
newc = Math.sqrt(newa + newb);
System.out.println("Your missing side is :: " + newc);
}
else
System.out.println("Sorry! There is an error!");
}
}

Cannot read the decimal point in Scanner

import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double pi = 3.142;
double T, v, a, x, r;
System.out.println("3.Centrifuge. The rotor of an ultracentrifuge rotates at x rev/s.");
System.out.println("A particle at the top of the test tube is r meter from the rotation axis.");
System.out.println("Calculate it’s centripetal acceleration.");
System.out.printf("Enter the number of rotation in rev/s : ");
x = input.nextInt();
System.out.printf("Enter the distance in meter : ");
r = input.nextInt();
T = 1/x;
v = (2*pi*r)/T;
a = (v*v)/r;
System.out.println("\n\nAnswer");
System.out.printf("The centripetal acceleration is : %.2f m/s^2\n", a);
}
}
Hi all. This is my coding and it cannot run when I put decimal point. how to fix it?
Replace input.nextInt(); with input.nextDouble();
x = input.nextInt(); // It will simply ignore decimal values
Hence you need to use nextDouble()
x = input.nextDouble(); // This will read the entire decimal value
You read a int with
input.nextInt();
Try
input.nextDouble();
Try it.
import java.util.Scanner;
public class JavaScannerDouble {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String data = input.next();
double decimalValue = Double.parseDouble(data);
System.out.println("Decimal value : " + decimalValue);
// test of decimal value
double response = decimalValue / 0.1;
System.out.println("response : " + response);
}
}

Categories

Resources