- 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!!
19 Antworten
- 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.........
+ 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();
}
}
+ 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();
}
}
+ 3
Dmitry Lezin, I can see your comment for putting the Math.PI first works but why is that?
+ 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();
}
}
+ 1
System.out.println(Math.PI*
(double)width*width
// * Math.PI // not at the end
);
+ 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
+ 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
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();
}
}
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();
}
}
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.
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;
}
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));
}
}
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();
}
}
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
- 1
Laia Higon You are welcome man..😊💐
Note: edit your question as solved and mark my answer as best.
- 1
type double is an approximation.
https://stackoverflow.com/questions/29122007/why-do-i-get-different-answers-multiplying-three-double-values-in-a-different-or
- 2
Laia Higon show me your attempt first..
- 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