Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: josephb401 en 25 de Septiembre 2015, 04:43
-
Hola a todos estoy teniendo un problema con este programa quisiera saber si alguien me puede ayudar a resolver este problema? El programa tiene 2 clases y la tercera tiene el main. Muchas gracias.
import java.util.Scanner; // needed for Scanner class
import java.text.DecimalFormat; // needed for DecimalFormat class
public class CarpetTester
{
public static void main(String[] arg)
{
double length;
double width;
double price;
Scanner keyboard = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("$#,##0.00");
System.out.print("Please enter the length of the room: ");
length = keyboard.nextDouble();
System.out.print("Please enter the width of the room: ");
width = keyboard.nextDouble();
System.out.print("Please enter the price per squart foot: ");
price = keyboard.nextDouble();
RoomDimensio dimension = new RoomDimensio(length, width);
RoomCarpet carpet = new RoomCarpet(dimension, price);
System.out.println("The total cost of the carpet is: " + df.format(carpet.getTotalCost() ) );
}
}
public class RoomDimensio
{
private double length;
private double width;
public RoomDimensio(double len, double wid)
{
len = length;
wid = width;
}
public double getArea()
{
double area = (length * width);
return area;
}
public String toString()
{
return "Length: " + length + "width: " + width +" Area: " + getArea();
}
}
public class RoomCarpet
{
private RoomDimensio dimension;
private double carpetCost;
public RoomCarpet(RoomDimensio dim, double cost)
{
dim = dimension;
cost = carpetCost;
}
public double getTotalCost()
{
double finalCost = (carpetCost * dimension.getArea());
return finalCost;
}
public String toString()
{
return "Total cost: " + getTotalCost();
}
}
-
Hola hice alguna modificaciones en tu código si por ahí te sirve te voy a enviar para comparar
con lo que tenes
clase principal main
package carpettester;
import java.text.DecimalFormat;
import java.util.Scanner;
public class CarpetTester {
public static void main(String[] args) {
double length;
double width;
double price;
double area;
double total;
Scanner keyboard = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("$#,##0.00");
System.out.print("Please enter the length of the room: ");
length = keyboard.nextDouble();
System.out.print("Please enter the width of the room: ");
width = keyboard.nextDouble();
System.out.print("Please enter the price per squart foot: ");
price = keyboard.nextDouble();
RoomDimensio dimension = new RoomDimensio();
area = dimension.RoomArea(length, width);
RoomCarpet carpet = new RoomCarpet();
total = carpet.getTotalCost(area, price);
System.out.println("The total cost of the carpet is: " + df.format(total));
}
}
clase para obtener la dimension
package carpettester;
public class RoomDimensio {
public RoomDimensio() {
}
public double RoomArea(double len, double wid) {
double area = (len * wid);
return area;
}
}
clase para obtener el total
package carpettester;
public class RoomCarpet {
public RoomCarpet() {
}
public double getTotalCost(double area, double price) {
double finalCost = (price * area);
return finalCost;
}
}