- 10

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!!

18th Dec 2020, 10:03 AM
Laia Higon
19 Respuestas
- 7
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.........
18th Dec 2020, 2:40 PM
NavyaSri
NavyaSri - avatar
+ 36
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(); } }
16th Feb 2021, 6:22 AM
Nishan Niriella
Nishan Niriella - avatar
+ 6
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(); } }
18th Dec 2020, 3:29 PM
Laia Higon
+ 3
Dmitry Lezin, I can see your comment for putting the Math.PI first works but why is that?
16th Apr 2021, 10:26 AM
Chris W
Chris W - avatar
+ 2
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(); } }
12th Dec 2021, 1:53 AM
MN FATHIMA RIHAM - s92064451 -321424451
MN FATHIMA RIHAM - s92064451 -321424451 - avatar
+ 1
System.out.println(Math.PI* (double)width*width // * Math.PI // not at the end );
16th Apr 2021, 8:18 AM
Dmitry Lezin
Dmitry Lezin - avatar
+ 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
24th Jun 2021, 3:44 PM
Dharmi Sri
Dharmi Sri - avatar
+ 1
IF YOU FAILED TEST 3 Do not use Math.pow(). Just calculate the area like this -> Math.PI*width*width; Here's my code => https://code.sololearn.com/c0dlJgy76kPz
18th Nov 2021, 9:23 PM
Jonas
Jonas - avatar
0
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(); } }
18th Dec 2020, 1:32 PM
Laia Higon
0
////////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(); } }
30th Mar 2021, 9:36 PM
Андрій Колодій
Андрій Колодій - avatar
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.
14th Aug 2021, 11:31 AM
Siyoum Tassew
Siyoum Tassew - avatar
0
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; }
10th Dec 2021, 10:13 AM
venkateswaramma.upputuri
0
import java.util.Scanner; //your code goes here public class Converter { public static String toBinary(int x){ String bin = ""; while(x > 0){ bin = (x%2)+bin; //Postfix x /= 2; } return bin; } } // It is printing in the reversce order, So let's reversce the String public class Program { public static void main(String[ ]args) { Scanner sc = new Scanner(System.in); int x sc.nextInt(); System.out.print(Converter.toBinary(x)); } }
14th Apr 2022, 6:47 AM
Xavier
0
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape { Square(int x) { this.width = x; } void area() { //return x * x; System.out.println(width * width); } } class Circle extends Shape { Circle(int y){ this.width = y; } public void area(){ System.out.println(Math.PI*width*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(); } }
30th Apr 2022, 10:03 AM
SILAMBARASAN G
SILAMBARASAN G - avatar
0
I DONT KNOW WHY But to pass TestCase 3 you have to make the Math.PI at first like =>>> Math.PI*width*width if you call it like this width*width*Math.PI it won't work. I still dont know the reason for this
20th Aug 2022, 9:55 PM
Mohammed Bedru
- 1
Laia Higon You are welcome man..😊💐 Note: edit your question as solved and mark my answer as best.
18th Dec 2020, 4:19 PM
NavyaSri
NavyaSri - avatar
- 2
Laia Higon show me your attempt first..
18th Dec 2020, 10:07 AM
NavyaSri
NavyaSri - avatar
- 3
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
9th May 2021, 10:09 AM
Pooja Manghani
Pooja Manghani - avatar