public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
i have declared the area as float earlier and i got complie time error for that, when i changed it into double, i can able to get the below output.
need to know why...??
float area;
Error:(24, 49) java: incompatible types: possible lossy conversion from double to float
Error:(27, 49) java: incompatible types: possible lossy conversion from double to float
Your code has several mistakes.
Please keep the naming conventions. Class starts with the capitalized letter, variable don't. So.. Triangle triangle1 = new Triangle().
In order to keep all the data in float, you need to multiply all the numbers in float as well. 0,5 is double as default, having the bigger decimal precision than float. There are two ways to fix that:
Use F or f indicator (I recommend the capitalized one) to say that you work with float. Otherwise Java unserstands it as double:
area = 0.5F * triangle1.length * triangle1.breadth;
Or cast all the resutl to float:
area = (float) 0.5 * triangle1.length * triangle1.breadth;
In Java by default number like 2.553 are double so when you do
area = 0.5 * Triangle1.length * Triangle1.breadth;
you are doing double * float * float. If you want to use 0.5 as float you need to add letter f after number so your code will looks like:
area = 0.5f * Triangle1.length * Triangle1.breadth;
You have two options:
Add f after every initialisation of float variables, like Triangle1.length = 11f;
Thus the code becomes:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; // declared area as float
Triangle1.length = 11f;
Triangle1.breadth = 22f;
Triangle2.breadth = 15f;
Triangle2.length = 20f;
area = 0.5f * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5f * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
Make the area variable double type:
Your code thus becomes:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
double area; // declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
Hope it helps!
Do this.
public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = (float)0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = (float)0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
Related
This is my last homework for my java class and I have been trying to run this through a compiler but I don't understand whats wrong with the code.
I tried using void after reading about how that would fix the return type issue but that just made it worse, maybe I was putting void in the wrong place.
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Rectangle() {
}
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " +
rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Rectangle rectangle2 = new Rectangle(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " +
rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
This is my last homework for this class I just want this over with any help is appreciated.
The constructor name should have the same name as the class name
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
}
public Rectangle() {
}
This is a method as far as Java is concerned. And all methods have to have a return type.
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
The same here.
You don't need the 1st one really unless you really need to be able to make one without those values set.
You can just rename them, but you probably just want to rename the class Rectangle
Whats wrong in your code is your class name and constructor names are different.
You have two options, one is to rename the constructor to Exercise01_01 or meke the return type of the Rectangle as void.
public class Exercise01_01 {
private double width = 1;
private double height = 1;
public Exercise01_01() {
}
public Exercise01_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise01_01 rectangle1 = new Exercise01_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + rectangle1.getPerimeter());
Exercise01_01 rectangle2 = new Exercise01_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + rectangle2.getPerimeter());
}
}
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise09_01 rectangle1 = new Exercise09_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Exercise09_01 rectangle2 = new Exercise09_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
Thanks for all the help and this is the code that passes through the compiler. Just leaving it here for future visitors.
I have tried this a couple of different ways. It is required I use the inheritance to extend these classes. Every time I run the program it just out puts 0.0 for volume and area. Radius displays correctly. Output at the bottom.
public class Base_HW04Q1
{
public double pi = 3.14, l, radius, height, area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
area = (radius * radius) * pi;
return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + area + ".";
}
}
public static class Cylinder extends Base_HW04Q1
{
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = 2 * pi * radius * height + 2 * pi * l;
return area;
}
public double calcVolume() {
volume = pi * (radius * radius) * height;
return volume;
}
public String toString() {
return "A Cylinder of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static class Cone extends Base_HW04Q1 //TODO: This line is almost, but not quite, complete.
{
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}
}
Output:
A Cylinder of radius: 30.0, area 0.0 and a volume of 0.0 A Round Shape
of radius: 50.0, area 0.0. A Cone of radius: 10.0, area 0.0 and a
volume of 0.0
Your toString() never calls the methods that do the calculations and instead prints the default 0.0 field values. You will run this risk if toString() is ever called before the calcXxxx() methods are called, i.e., before the calculated fields have been given a decent value. The best solution is to prevent this problem from happening in the first place by entirely getting rid of fields for calculated values, for instance area and volume. Instead within toString(), call the methods to get these values.
e.g.,
public double pi = 3.14, l, radius, height; // , area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
return (radius * radius) * pi;
// return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + calcArea() + ".";
}
}
It's because you only instantiate the object and entered to constructor but not in the other method.
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
no one called in these methods
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
and the rest methods. Call them from constructor or from main if you want to calculate through method like:
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
double roundArea = RoundShape1.calcArea();//then use this
string roundMessage = RoundShape1.toString();//and this whatever you want.
//do it in others too
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}
I got the assignment to write a Java program that gives the circumference and area of a circle of radius 1 through 50. This is what I've got:
public class Circles {
public static void main(String[] args) {
}
{
for (int i = 1; i <=50; i=i+1)
area = PI * (radius * radius);
System.out.println("The area is " + area);
double circumference= PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}
It says that radius, pi, and area cannot be resolved to a variable.
Because you never declared them as variables (nor, in the cases of PI and radius, even give them values). Notice that it did not complain about circumference, which you did declare.
You have to declare your variables - at the moment you're only declaring two of them, namely i and circumference. Also, Java has a PI constant which is found in the Math class, so you'll need Math.PI:
public static void main(String[] args) {
for (int i = 1; i <=50; i=i+1) {
double radius = i;
double area = Math.PI * (radius * radius);
System.out.println("The area is " + area);
double circumference = Math.PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}
public class Circles {
public static void main(String[] args) {
double PI = Math.PI;
for (int radius = 1; radius <=50; i=i+1) {
double area = PI * (radius * radius);
System.out.println("The area is " + area);
double circumference= PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}
Ok so, I'm learning java using a book I bought online, and for some reason Java wont allow this program to work even though it's the exact text from the book. Can someone explain why I keep getting told 'The type SimpleCircle is already defined'? it shows this as an error next to the line "SimpleCircle() { radius = 1; }"
public class SimpleCircle {
/** Main method */
public static void main(String[] args) {
//create a circle with radius 1
SimpleCircle circle1 = new SimpleCircle();
System.out.println("The area of the circle of radius "
+ circle1.radius + " is " + circle1.getArea());
//create a a circle with radius 25
SimpleCircle circle2 = new SimpleCircle(25);
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());
//create a circle with radius 125
SimpleCircle circle3 = new SimpleCircle(125);
System.out.println("the area of the circle of radius "
+ circle3.radius + " is " + circle3.getArea());
//modify circle radius
circle2.radius = 100;
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());
}
double radius;
/** construct a circle with radius 1 */
SimpleCircle() {
radius = 1;
}
/** construct a circle with a specified radius */
SimpleCircle(double newRadius) {
radius = newRadius;
}
// return the area of this circle
double getArea() {
return radius * radius * Math.PI;
}
// return the perimeter of the circle
double getPerimeter() {
return 2 * radius * Math.PI;
}
// set a new radius for this circle
void setRadius(double newRadius) {
radius = newRadius;
}
}
I too faced the same error.The reason for this is the folder in which you are working is contained with another file(other than the present working file) in which the same class(here SimpleCircle) was used.Simply modify the class name(eg: SimpleCircle to SimpleCircle2) in the present file, then the error will be gone.
I can't seem to figure out why my coneVolume method is returning zero when all of my other methods are working properly.
import java.util.Scanner;
public class P56old{
public static double sphereVolume(double r){
double sphereVolume = (4/3)*(Math.PI)*(Math.pow(r, 3));
return sphereVolume;
}
public static double sphereSurface(double r){
double sphereSurface = 4 * (Math.PI) * Math.pow(r, 2);
return sphereSurface;
}
public static double cylinderVolume(double r, double h){
double cylinderVolume = (Math.PI) * (Math.pow(r, 2)) * h;
return cylinderVolume;
}
public static double cylinderSurface(double r, double h){
double cylinderSurface = 2 * (Math.PI) * (Math.pow(r, 2)) + 2 * Math.PI * r * h;
return cylinderSurface;
}
public static double coneVolume(double r, double h){
double coneVolume = (1/3) * Math.PI * (Math.pow(r,2)) * h;
return coneVolume;
}
public static double coneSurface(double r, double h){
double s = Math.sqrt(Math.pow(r,2) + Math.pow(h, 2));
double coneSurface = Math.PI * Math.pow(r,2) + Math.PI * r * s;
return coneSurface;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please give the radius: ");
double r = in.nextDouble();
System.out.print("Please give the height: ");
double h = in.nextDouble();
double coneVolume = coneVolume(r,h);
double sphereVolume = sphereVolume(r);
double sphereSurface = sphereSurface(r);
double cylinderVolume = cylinderVolume(r,h);
double cylinderSurface = cylinderSurface(r,h);
double coneSurface = coneSurface(r,h);
System.out.println("The Sphere Volume is " + sphereVolume);
System.out.println("The Sphere Surface is " + sphereSurface);
System.out.println("The Cylinder volume is " + cylinderVolume);
System.out.println("The Cylinder Surface is " + cylinderSurface);
System.out.println("The Cone Volume is " + coneVolume);
System.out.println("The Cone Surface is " + coneSurface);
}
}
I'd appreciate any insight on the matter, and any critique is appreciated. I think it may have to do with all the public classes and maybe another method is affecting the coneVolume method but I just don't know enough about methods at the moment to fix the issue at hand.
When you do 1/3, it does integer division, resulting in 0 (the remainder is 1). Multiplying by 0 gives 0. Do 1.0/3.0 instead, and it will correctly compute an approximation to one third.