Input Length And Width - Calculate And Print Perimeter And Area Of A Rectangle
Feb 16, 2015
Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input rectangle. All methods should be tested in the runner class.
This is my code:
import java.util.Scanner;
public class Rectangle {
double length;
double width;
public Rectangle() {
[Code] ...
What have I done??? I have created this program using the few different resources with which I am supplied, but I don't understand the resources.
View Replies
ADVERTISEMENT
Apr 27, 2014
I am trying to make a program so that the user has to enter a number for the width and length and it will give the area and perimeter:
import java.util.* ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class jframe extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
[Code] ....
It is giving me an error saying that the ExitButtonHandler and ebHandler do not have classes but I don't understand why.
View Replies
View Related
Mar 19, 2014
I was suppose to create a simple Java program for calculating the area of a rectangle (height * width). Then check the user’s input, and make sure that they enter in a positive integer, and letting them try again if they enter in a negative number. (I'm not sure how to get them to try again.
I am suppose to use an "if" statements and indeterminate loops to achieve the solution. The program will have the following requirements:
1. Ask the user to enter in both a height and width (as an integer)
2. If the user enters in a negative number, display an error
3. If the user enters in a negative number, give them another chance to enter in the correct value
4. Calculate the area and display to the screen once two positive integers have been entered.
import java.util.Scanner;
public class RectangleAreaCalc
{
public static void main(String[] args)
{
int length;
int width;
int area;
[Code] ....
View Replies
View Related
Sep 17, 2014
This program is for a swimming pool filling service company. They charge 2 cents per gallon and $50 per hour to fill a pool. The truck can fill at a rate of 730 gallons per hour.
Create the Pool class that calculates the cost to fill a pool based on it's Shape, length, width and depth. (Input order is S L W D)
The pool class will need data fields for String shape, double length, double width, double depth, static double GallonsPerSqFoot = 7.4805, static double price per gallon = .02, static double FillingFeePerHour = 50.0, and static double FillingRate = 730gal/hr.
Create a No-Arg constructor and a constructor that accepts the non-static values, and has the methods: getShape, getLength, getWidth, getDepth, getGallons, getHours, getFillingFeePerHour, getHourlyCost and getTotalCost.
At the end of the class, create a main() method that asks for the input and returns the output based on the Pool class gets.. methods.
The shape options are oblong or rectangle. (A round pool would be oblong with the same width and height, a square pool would have the same width and height)
Formulas:
Rectangle cubic ft = length * width * depth.
Oblong cubic ft= ((Math.PI * Math.pow(width/2,2)* depth) +((length-width) * width * depth))
Gallons = cubic ft * 7.4805.
hours = total gallons/730.
Total cost = (total Gallons * .02)+(hours * $50)
Example Output
An oblong pool 18.00 feet long by 12.00 feet wide and 5.00 feet deep will use 6923.10 gallons of water and take 9.48 hours to fill. The total cost will be 612.65.
Here is my code:
import java.util.Scanner;
import java.util.*;
public class Pool
{
private String poolshape1 = "oblong";
private String poolshape1 = "rectangle";
private double length;
private double width;
private double depth;
private static double GallonsPerSqFoot = 7.4805;
[Code] .....
View Replies
View Related
Mar 3, 2015
package areatest;
import javax.swing.JOptionPane;
public class AreaTest {
public static double areaTriangle (double length, double width){ //How to calculate the area of a triangle
return .5f * length * width;
[Code] .....
When I try to get the area of a rectangle it gives me 9 no matter what input I give it. When I try to get the area of a triangle it gives me .5 no matter what input I give it. Same with the circle but it always gives me 12.56370...
View Replies
View Related
Sep 1, 2014
import java.lang.Math;
public class triangle {
double area; double s;
public double findArea(double sideA, double sideB, double sideC);
{
s= 0.5 *(sideA + sideB + sideC);
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
[Code] ...
Errors I am getting:
Triangle.java:23: error: class, interface, or enum expected
import java.util.Scanner
^
Triangle.java:25: error: class, interface, or enum expected
public static void main(String[] args) {
[Code] ....
View Replies
View Related
Mar 22, 2015
I've been given a school assignment that reads, "Rewrite the main class Geometry so it takes in the dimensions for the triangle and ellipse as user inputs and create a Triangle and an Ellipse class. Use the appropriate variable types, constants, variable names and computational formulas.
Triangle class will have a computePerimeter and a computeArea methods Ellipse class will have a computeArea method Create Report class
• Create a method createReport that takes the values returned from Triangle and Ellipse and combines them in the following message and displays it. Format the values so that they have 2 decimals.
“The triangle has a perimeter of [perimeter] with an area of [area] while the ellipse has an area of [area]”
• Create a method switchReport that takes the original string from createReport and changes the message to display using the available methods in the String class
“The ellipse has an area of [area] while the triangle has an area of [area] with a perimeter of [perimeter]”"
I've run into a problem when creating the createReport method. Everytime i run it i get incorrect values for the perimeter and area (namely i get zero every time).
my code is as follows:
public class Triangle
{
public double base;
public double height;
public double hypotenuse;
private double tArea;
private double perimeter;
public Triangle()
{
base = 0;
height = 0;
hypotenuse = 0;
[code]....
For the triangle class and
public class Report {
Triangle tri2 = new Triangle();
Ellipse eli2 = new Ellipse();
public Report() {
}
public void createReport() {
System.out.println("The triangle has a perimeter of "+tri2.computePerimeter() +" with an area of " +tri2.computeTArea() +" while the ellipse has an area of " +eli2.computeEArea() );
}
for the report class.the Geometry class allows you to input values and if i skip the report and simply print the perimeter and area they are correct. However with the report class it simply gives me zeros.
View Replies
View Related
Sep 28, 2014
I am also having trouble with another program.
Java Code:
import javax.swing.JOptionPane;
import java.util.*;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
public class ShelbyHarms_3_03 {
public static void main (String [] args) {
double a, b, c; //Input sides of triangle
double x; //Perimeter of triangle
double area; //Area of triangle
[Code] .....
Here are the errors:
ShelbyHarms_3_03.java:39: error: variable x might not have been initialized
JOptionPane.showMessageDialog(null, formatter.format(x));
^
ShelbyHarms_3_03.java:42: error: variable area might not have been initialized
JOptionPane.showMessageDialog(null, formatter.format(area));
^
2 errors
Am I not formatting the decimal format right?
View Replies
View Related
Mar 19, 2015
What is happening in below mentioned java code. Why in method insert, int l, int w is declaring & it is assigning to length & width.
class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
[Code] ....
View Replies
View Related
May 17, 2013
I do not understand the getActionCommand and ActionListener concepts. This code does not resolve the volume for length, width, and height.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.awt.Container;
public class rectangularSolid extends JApplet implements ActionListener {
[Code] ....
View Replies
View Related
May 19, 2014
I started with finding the area of a triangle, but now I'm trying to ask a user what kind of shape they want the area for, then ask questions to get the area. I can't figure out how to take the shape a person types to go to a certain case. It also says shape hasn't been initialized. I don't know how to do that.
import java.util.Scanner;
public class TriangleArea {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
char shape;
String text = "Do you want to find the area of a triangle, square, rectangle, or trapezoid?";
System.out.print("Text");
switch(shape){
[code]....
View Replies
View Related
Mar 6, 2014
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Assignment extends Applet implements ActionListener {
TextArea textInput = new TextArea(); // user input
Button analyzebutton = new Button("Analyze");
Button resetbutton = new Button("Reset");
Label lbloutput = new Label ("Please enter text into the textbox!");
[Code] .....
View Replies
View Related
Mar 11, 2015
I'm starting with my version of very basic program: calculating area of circle. And of course it doesn't get well. My question: what is wrong in this code?
public class circleAre{
double radious;
void putData(double radi){
radi = radious;
[Code] .....
View Replies
View Related
Sep 24, 2014
My homework is to find the surface area and volume of a cone. I have 2 individual java program for the homework and one them already works.
public class Cone {
Cone() {}
double r, h;
double volume() {
return (1.0 / 3.0) * Math.PI * r * r * h;
[Code] ....
The second one I keep getting error mainCone.java:22: error: reached end of file while parsing
public class Conemain {
public static void main(String [] args) {
Cone = new Cone();
Cone.setR(5);
Cone.setH(8);
[Code] ....
I need fixing the second one in order to find the surface area and the volume for cone.
View Replies
View Related
Apr 23, 2014
I am not getting any syntax errors but the program simply does nothing when I run it.
I literally just get "run: BUILD SUCCESSFUL (total time: 3 seconds)".
So basically, I need to create a program that calculates the price of carpeting for rectangle rooms.(multiply the area of the floor by the price per square foot of the carpet).
-The RoomDimension class should have two fields: one for length and one for width, and a method that returns the area of the room
-The RoomCarpet class that has a RoomDimension object as a field, a field for the cost of carpet per square foot, and a method that returns the total cost of the carpet
Here is what I have:
package roomdimension.java;
public class RoomDimension {
private double length=0;
private double width=0;
private final double area = length * width;
public void RoomDimension(double len, double w)
[Code] ....
View Replies
View Related
Mar 5, 2014
I am doing an assignment that is asking for the user to put in the radius of a circle and the program figures out the area, diameter and circumference. It is using 2 different java programs to accomplish this. One with the info on how to get area, diameter and circumference and one is the demo that runs the program. I keep getting errors on my demo.
// Circle Class
public class Circle {
private double rad;
private double Pie;
private double area;
private double diameter;
[Code] .....
View Replies
View Related
Feb 24, 2014
Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. Where the '?' is stated.
import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
double a;
double b;
double c;
double s;
double x;
double area;
[Code] ....
View Replies
View Related
Sep 9, 2014
Write a Java program that calculates the area and volume of a cube, sphere, cylinder, and regular tetrahedron. Your program should obtain the necessary input for each of the objects from the console and output the input, area, and volume of the object. Use appropriate messages to guide the user of your program.
View Replies
View Related
Feb 20, 2014
In a forest, there are some bamboo trees .The length of each tree get doubled during winter and increases by one unit in summer , write a Java program to calculate the total length of n number of bamboo trees in M number of seasons. The season always starts with winter.
import java.util.Scanner;
public class Tree {
public static void main(String args[]) {
int length;
int season;
[Code] ....
View Replies
View Related
Apr 7, 2014
I have a popup that displays textual info and sometimes gets text input from the user. I'm using the following code:
JTextArea textArea = new JTextArea( getGameHistory( true, -2 ) );
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("courier", Font.PLAIN, 12));
scrollPane.setPreferredSize( new Dimension( 30, 100 ) );
String input = JOptionPane.showInputDialog(null, scrollPane, "Fun Chess ",
JOptionPane.YES_NO_OPTION);
This shows a nice text area and an input field underneath it.The getGameHistory() method just returns a long string detailing the current game.Is there a way of just showing the text area without the input field underneath?Can the text area also be used for input instead?
View Replies
View Related
Nov 11, 2014
I am stuck on one of the projects: Area Calculator
I am able to code an area calc that works, but am unsure how to move all the input/output to main like the project wants. here is a piece of my code so far:
public static void main( String[] args )
{
Scanner kb = new Scanner(System.in);
int base = 0, height = 0, width = 0, radius = 0, choice;
System.out.println( "-=-=-=-=-=-=-=-=-=-=-" );
System.out.println( "1. Triangle" );
System.out.println( "2. Rectangle" );
System.out.println( "3. Square" );
System.out.println( "4. Circle" );
System.out.print( "Pick a shape: " );
[Code] ....
I have the same thing for rectangle, square, and circle (of course with the correct formulas) and the program works perfect, but the assignment is still wrong until I can move the input/output to main. what do I do?
View Replies
View Related
Sep 30, 2014
The problem I'm trying to resolve now is getting my ActionEvent to send the value input in the JTextArea out to be calculated, then to display the result in my result JLabel.
Here is an image of the GUI :
The basic procedure the teacher is asking for is the user is supposed to enter a temperature in the input. The user then clicks the input scale, and then on the output scale selection the converted value is to be displayed in the output area.
As I mentioned my problem is in the sending of the input value to my calculation area, and getting the value to display in the JLabel.
How should I approach this solution? Do I need to have a listener after the JTextArea of the input box? Will that allow me to limit the input values to numbers only?
final JTextArea inputText = new JTextArea("" + (char)176,1,4);
//ReadConsole equivalent to specific input was a number?
Here is the section of code my ActionListener is:
//celOut represents the Celcius output scale JRadioButton.
celOut.addActionListener (new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {
//if the Celcius Input Scale is selected.
if(cel.isSelected())
[Code] ....
All the calculations are required to occur in a separate java file, which what I think is tripping me up. How do I send a value from the JTextArea into the Calc.celToFahr method?
//contents of my Calc.java calculation class.
public class Calc {
public static double celToFahr(double cel){
return cel * (9./5.) + 32.;
}
public static double fahrToCel(double fahr){
return (fahr - 32.)*(5./9.);
}
}
View Replies
View Related
Nov 19, 2014
I have to use methods and loops to code this program. Cannot use any java set or any built in java shortcuts!
import java.util.Scanner;
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
[Code] .....
This is the input I put in my code and this is the output:
How many numbers will be in the 1st set: 3
Enter list of integers for 1st set:
34
2
56
The ascending order for 1st is:
2
34
56
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set:
56
2
33
6
The ascending order for the 2nd set is:
2
6
33
56
The intersection of the two sets is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The difference of A-B is:
View Replies
View Related
Nov 11, 2014
I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.
I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?
This is what I have now.
for (Parties p : h.geral) {
show += String.format("Constituency - %5s - %5s - %d%n",
p.getConstituency(), p.getParty(), p.getNum_seats());
}
View Replies
View Related
Nov 19, 2014
As part of the instructions we are required to use loops and methods to solve to program this code. We are not allowed to use any set functions that are already built in java.The methods for intersection and difference MUST return int value.
import java.util.Scanner;
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
[code]...
View Replies
View Related
Nov 6, 2014
package p6;
import java.util.Scanner;
public class Bill {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
//final variables Premium Service
final double monthlyChargeP = 25.00;
final double FordayminutesP = 0.10;
[Code] ....
I can't find the syntax on {} my block is highlighted red in my application
View Replies
View Related