SOLVED Exercice Shapes in java | Sololearn: Learn to code for FREE!

-5

SOLVED Exercice Shapes in java

Hello everybody! I do not know how to do the exercice Shapes in the course of Java , some ideas? Thank you!!

12/18/2020 10:03:20 AM

Laia Higon

13 Answers

New Answer

-1

Laia Higon How can I told you ......uff......Your code Shows error here change "printl" to "println" .. another thing there is no need to import javax.swing....// remove it. and also int width1 ; variable in abstract class Shape is also no need to define. why because you already defined individual variables in Square and Circle class. so pls...remove it. area()method of Square class will always give you 25 .why because you defined width=5*5 // change it to int width1= width*width in area() method of Circle class want you a double value..so, pls change int to double.. I.e., double width1= k*width*width; Hope you understand...πŸ™πŸ™πŸ™ .........H A P P Y C O D I N G.........

+8

now i have it!! thanky u import java.util.Scanner; abstract class Shape { int width; abstract void area(); double k = Math.PI; int width1; double width2; } //your code goes here class Square extends Shape{ public Square (int width){ this.width = width; } void area (){ int width1= (width*width); System.out.println (width1); } } class Circle extends Shape{ public Circle (int width){ this.width = width; } void area(){ double width2 = ((k)*width*width); System.out.println (width2); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

+4

Laia Higon You are welcome man..πŸ˜ŠπŸ’ Note: edit your question as solved and mark my answer as best.

+4

import java.util.Scanner; import java.lang.Math.*; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ public Square(int width){ this.width = width; } public void area(){ int area = this.width * this.width; System.out.println(area); } } class Circle extends Shape{ public Circle(int width){ this.width = width; } public void area(){ System.out.println((double)Math.PI * this.width * this.width); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

+3

System.out.println(Math.PI* (double)width*width // * Math.PI // not at the end );

+2

Dmitry Lezin, I can see your comment for putting the Math.PI first works but why is that?

+1

import java.util.Scanner; import static javax.swing.Spring.width; import static java.lang.Math.PI; abstract class Shape { int width; abstract void area(); double k = Math.PI; int width1; } //your code goes here class Square extends Shape{ public Square (int width){ this.width = width; } void area (){ int width= 5*5; System.out.printl (width); } } class Circle extends Shape{ public Circle (int width){ this.width = width; } void area(){ int width1 = (int) ((k)*width*width); System.out.printl (width1); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

+1

////////Why this code can't go through the Test3? mport java.util.Scanner; import java.lang.Math; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public Square(int width){ this.width=width; } public void area(){ System.out.println(this.width*this.width); } } class Circle extends Shape{ public Circle(int width){ this.width=width; } public void area(){ System.out.println((this.width*this.width)*Math.PI); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

+1

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public int area2; public Square(int x) { width=x; } public void area() { area2=width*width; System.out.println(area2); } } class Circle extends Shape { public double area1; public Circle(int y) { width=y; } public void area() { area1=Math.PI*width*width; System.out.println(area1); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }//Without using this

0

Laia Higon show me your attempt first..

0

type double is an approximation. https://stackoverflow.com/questions/29122007/why-do-i-get-different-answers-multiplying-three-double-values-in-a-different-or

0

Fill in the blanks to inherit the Square class from the Shape class and implement the area method, which should output the area of the square.

-2

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); for (int x = 0; x <3; x++){ int actual_amount = (amount * 10)/100; amount = amount - actual_amount; } System.out.println(amount); } } This absolute correct code for loan calculator in sololearn