+ 4

Abstract classes

Why would this not pass all the cases?!?! import java.util.*; import java.lang.*; abstract class Shape { int width; abstract void area(); } //your code goes here public class Square extends Shape { Square(int w) { width = w; } public void area(){ System.out.println(width*width); } } public class Circle extends Shape { Circle(int w) { width = w; } public void area(){ int sq = width*width; System.out.println(Math.PI*sq); } } 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(); } }

27th Nov 2020, 5:42 PM
Roderick Davis
Roderick Davis - avatar
3 Answers
27th Nov 2020, 6:47 PM
Avinesh
Avinesh - avatar
+ 1
dont see why it wouldn’t work the same my way, thanks.
27th Nov 2020, 7:05 PM
Roderick Davis
Roderick Davis - avatar
+ 1
class Main { public static void main(String[] args) { //do not touch this code Monopoly monopoly = new Monopoly(); Chess chess = new Chess(); Battleships battleships = new Battleships(); monopoly.play(); chess.play(); battleships.play(); } } abstract class Game { public String name; abstract String getName(); abstract void play(); } class Monopoly extends Game { //give "Monopoly" name to game String getName() { return name; } // play method should print "Buy all property." void play() { System.out.println("Buy all property."); } } class Chess extends Game { //give "Chess" name to game String getName() { return name; } // play method should print "Kill the enemy king." void play() { System.out.println("Kill the enemy king."); } } class Battleships extends Game { //give "Battleships" name to game String getName() { return name; } // play method should print "Sink all ships." void play() { System.out.println("Sink all ships."); } }
7th Oct 2022, 1:42 PM
Kavibharathi Sokkanathan