question archive Shapes You are working on a graphical app, which includes multiple different shapes
Subject:Computer SciencePrice:4.87 Bought7
You are working on a graphical app, which includes multiple different shapes.
The given code declares a base Shape class with an abstract area() method and a width attribute.
MAke a two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods.
The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width).
The code in main creates two objects with the given user input and calls the area() methods.
Sample Input:
5
2
Sample Output:
25
12.566370614359172
The area of the square is 5*5=25, while the area of the circle is PI*2*2=12.566370614359172
Use the Math.PI constant for the area calculation of the circle.
The binary numeric system uses only two digits: 0 and 1. Computers operate in binary, meaning they store data and perform calculations using only zeros and ones.
Make a program to convert integer numbers to their binary representation.
Make a Converter class with a static toBinary() method, which returns the binary version of its argument.
The code in main takes a number as input and calls the corresponding static method. Make sure the code works as expected.
Sample Input:
42
Sample Output:
101010
You can use the following code to convert a number to binary:
String binary="";
while(num > 0) {
binary = (num%2)+binary;
num /= 2;
}
JAVA
The code above uses a loop to convert num to binary and stores the result in the binary String.
Answer:
Here are both the required codes implemented in Java, having:
Question 1 Shapes
Java Codes:
Shape.java file
public abstract class Shape { // To define an abstract Base class named as Shape double width; public abstract void area(); // abstract area() method }
Square.java file
import java.util.Scanner; public class Square extends Shape { // To create a Square class which is a subclass of Shape class @Override public void area() { // Implement the area() method System.out.println(this.width * this.width); // Output the area of Square } public Square(double width) { this.width = width; // Initialize the width variable using Constructor } public static void main(String[] args) { /* To take the width from user*/ Scanner scn = new Scanner(System.in); double width; System.out.println("Enter the Width:"); width = scn.nextDouble(); Square sq = new Square(width); // Create an object of Square class sq.area(); // Call area() method } }
Sample Input and Output:
Enter the Width:
5
25.0
Circle.java file
import java.util.Scanner; public class Circle extends Shape { // To create a Circle class which is a subclass of Shape class @Override public void area() { // Implement the area() method System.out.println(Math.PI * this.width * this.width); // Output the area of Circle } public Circle(double width) { this.width = width; // Initialize the width variable using Constructor } public static void main(String[] args) { /* To take the width from user*/ Scanner scn = new Scanner(System.in); double width; System.out.println("Enter the Width:"); width = scn.nextDouble(); Circle c = new Circle(width); // Create an object of Circle class c.area(); // Call area() method } }
Sample Input and Output:
Enter the Width:
2
12.566370614359172
Question 2. Binary Converter :
Converter.java file:
import java.util.Scanner; public class Converter { /* To define a toBinary() method that will take integer as an argument and returns the binary string representation of given number */ public static String toBinary(int num) { String binary=""; while(num > 0) { binary = (num % 2) + binary; num /= 2; } return binary; } public static void main(String[] args) { /* To take the number from user*/ Scanner scn = new Scanner(System.in); int num; System.out.println("Enter a number:"); num = scn.nextInt(); System.out.println(toBinary(num)); // Call toBinary() and print the binary representation of given number } }
Sample Input and Output:
Sample 1
Enter a number:
42
101010
Sample 2
Enter a number:?
71
1000111
Step-by-step explanation
Here are the Step by step explanations of working of each Codes.
Question 1
Question 2
1. Here first we have created a Converter class and defined a static method as ,
/* To define a toBinary() method that will take integer as an argument and returns the binary string representation of given number */ public static String toBinary(int num){}
2. Next we have created a main() method and inside main() we have asked the user to enter the number, after that we have passed the same number to the toBinary() method and printed its equivalent binary representation on screen.
3. Finally we have printed the output on screen.
"Having any doubts regarding the working of this code or anything else which is not being understood then please before rating the solution, feel free to ask me in comment section"
further for better understanding of codes I have provided the comments in the codes itself, provided sample input and output with step by step explanations of each codes so as to understand the working of codes properly .
Shape.java
Circle.java
Converter.java
please use this google drive link to download the answer file.
https://drive.google.com/file/d/1X5vsF8t1gnZRRvjd4_BQzjRn8Vw_9rfM/view?usp=sharing
note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.
https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link