Question

this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

this is a jave program please help, I'm so lost

import java.util.Scanner;

public class TriangleArea {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
  
Triangle triangle1 = new Triangle();
Triangle triangle2 = new Triangle();

// Read and set base and height for triangle1 (use setBase() and setHeight())
  
// Read and set base and height for triangle2 (use setBase() and setHeight())
  
// Determine larger triangle (use getArea())
  
private int base;
private int height;
private int area;

public Triangle(int b, int h){
// Assign values passed to the constructor to the variables
base = b;
height = h;
  
area=(base*height)/2;
}
public int getBase(){
return base;
}
public void setBase(int b){
base =b;
}
public void print(){
System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
}
}
  
System.out.println("Triangle with larger area:");
// Output larger triangle's info (use printInfo())
  
}
}

public class Triangle {
private double base;
private double height;

public void setBase(double userBase){
base = userBase;
}
public void setHeight(double userHeight) {
height = userHeight;
}

public double getArea() {
double area = 0.5 * base * height;
return area;
}

public void printInfo() {
System.out.println("Base: " + base);
System.out.println("Height: " + height);
System.out.println("Area: " + getArea());
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Scanner;
class Triangle {
private double base;
private double height;
private double area;

public Triangle(int b, int h){
// Assign values passed to the constructor to the variables
base = b;
height = h;
  
area=(base*height)/2;
}
public Triangle(){
// Assign values passed to the constructor to the variables
base = 0;
height = 0;
  
}

public double getBase(){
return base;
}
public double getHeight(){
return height;
}
public double getArea() {
double area = 0.5 * base * height;
return area;
}
public void setBase(double userBase){
base = userBase;
}
public void setHeight(double userHeight) {
height = userHeight;
}
public void print(){
System.out.println("The area of a triangle with the base" +base+ "and the height" +height+ "is: " +area);
}


}

public class TriangleArea {
public static void printInfo(Triangle triangle1,Triangle triangle2) {
System.out.println("Triangle with larger area:");
double base,height,area;
  
if(triangle1.getArea()>triangle2.getArea())
{
base=triangle1.getBase();
height=triangle1.getHeight();
area=triangle1.getArea();
}
else
{
base=triangle2.getBase();
height=triangle2.getHeight();
area=triangle2.getArea();
}
System.out.println("Base: " + base);
System.out.println("Height: " + height);
System.out.println("Area: " + area);
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
  
Triangle triangle1 = new Triangle(3,5);
Triangle triangle2 = new Triangle(3,7);

triangle1.setBase(3);
triangle1.setHeight(5);
triangle2.setBase(3);
triangle2.setHeight(7);

printInfo(triangle1,triangle2);


}
  
// Output larger triangle's info (use printInfo())
  
}


Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and...

    Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle with larger area: Base: 4.00...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ brea...

    ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ break continue) 2- Functions 3- Arrays 4- pointers and strings 5- classes 6- file processing ))) if it was a program i need to see the code printed on computer and the output too please . 10. Write a C++code that include two classes: Rectangle and Triangle as shown below: #include <iostream> using...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • C++ The Shape.hcontains the information of the length of an edge. The derived triangle class has...

    C++ The Shape.hcontains the information of the length of an edge. The derived triangle class has the length of the height of the corresponding edge. Use the two values to compute the area of this triangle in getarea member function.Create the instance and output its result in the main function. Triangle.cpp: #include "triangle.h" Triangle ::Triangle() { //default constructor } float Triangle ::getarea() { //compute the area of the shape } void Triangle ::setheight( float h ) { //set the height...

  • *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram...

    *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram {     public static void main(String[] args) {         Scanner scnr = new Scanner(System.in);         int numCount = scnr.nextInt();         int[] Array = new int[numCount];                for(int i = 0; i < numCount; ++i) {             Array[i] = scnr.nextInt();         }         int jasc = Array[0], gws = Array[1], numbers, tempo;         if (jasc > gws) {             tempo = jasc;             jasc...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT