JAVA
A Matryoshka, or Russian nesting doll is either solid or hollow. If it is hollow, it contains another doll (which can also be hollow or solid). Given the supplied Doll class, implement a recursive method named show that returns a String representing the chain for dolls, for example
Natasha contains Tanya contains Peter
public class Doll
{
private String name;
private Doll dollInside;
/**
* Constructs a hollow doll with the doll it contains.
*/
public Doll(String theName, Doll theDoll)
{
name = theName;
dollInside = theDoll;
}
/**
* Constucts a solid doll.
*/
public Doll(String theName) { name = theName; dollInside = null; }
public boolean isSolid() { return dollInside == null; }
public String getName() { return name; }
public Doll getDollInside() { return dollInside; }
}
public class DollViewer
{
public static String show(Doll d)
{
/* code goes here */
}
public static void main (String [] args)
{
Doll dolls1 = new Doll("Alina", new Doll("Vera", new Doll("Galina")));
Doll dolls2 = new Doll("Anna", new Doll("Dina"));
Doll dolls3 = new Doll("Nika");
Doll dolls4 = new Doll("Martina", new Doll("Olga", new Doll("Sofia", new Doll("Elvira"))));
System.out.println(show(dolls1));
System.out.println(show(dolls2));
System.out.println(show(dolls3));
System.out.println(show(dolls4));
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 1 images
- 2arrow_forwardPoker card in PythonLet the CarteBase class present itself in the context of this exercise. It encapsulates a game card characterized by numerical values for its face and its kind. The possible faces are integers from 1 to 13, with face 1 corresponding to the ace, and faces 11, 12 and 13 corresponding respectively to the jack, the queen and the king. The other faces are simply identified by their number from 2 to 10. The possible grades are integers from 1 to 4 corresponding respectively to the cards of spade, heart, tile and clover. Analyze this class to understand how it works. You are asked to create a class called CarteDePoker that inherits CarteBase and defines or redefines the following methods: force(self) which returns the strength of the card in the poker game. In Poker, cards have the strength of their digital face from 2 to 13, except for the ace which is stronger than the other cards. So we give it a strength of 14.__eq__(self, other) to define operator behavior…arrow_forwardUSING C++ Implement the Rectangle class below: *******RECTANGLE CLASS******* class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; *******END OF RECTANGLE CLASS******* Enhance the Rectangle class with two new member functions: double getPerimeter() - returns the perimeter of the rectangle, i.e. 2*width+2*length bool isSquare() - returns true if the rectangle is a square, i.e. length is equal to width, returns false if not Demonstrate your class works with the main function that instantiates a Rectangle object and shows sample output for these functions workingarrow_forward
- Write in Javaarrow_forwardApex(Salesforce) Write the Apex Test Class for the Below Apex Method and also show the code Coverage Output Here is the Apex code: - public with sharing class MyFirstClass { public static Integer performMultiplication(Integer int1, Integer int2) { //Create a variable for checking null values Integer int3=1; //Check if both the integer not null or have some value in it if(int1!=null && int2!=null) { int3 = int1*int2; } else if (int1==null && int2!=null) { int3 = int2; } else if(int1!=null && int2==null) { int3=int1; } return int3; }}arrow_forwardSee ERROR in Java file 2: Java file 1: import java.util.Random;import java.util.Scanner; public class Account {// Declare the variablesprivate String customerName;private String accountNumber;private double balance;private int type;private static int numObjects;// constructorpublic Account(String customerName, double balance, int type) { this.customerName = customerName;this.balance = balance;this.type = type;setaccountNumber(); // set account number functionnumObjects += 1;}private void setaccountNumber() // definition of set account number{Random rand = new Random();accountNumber = customerName.substring(0, 3).toUpperCase();accountNumber += type;accountNumber += "#";accountNumber += (rand.nextInt(100) + 100);}// function to makedepositvoid makeDeposit(double amount){if (amount > 0){balance += amount;}}// function for withdrawboolean makeWithdrawal(double amount) {if (amount < balance)balance -= amount;elsereturn false;return true;}public String toString(){return accountNumber +…arrow_forward
- HW 4 Code (In Java) import java.util.Scanner;class Movie {private String name;private String mipaaRating;private int terrible;private int bad;private int ok;private int good;private int great;public Movie(String name, String mipaaRating) {this.name = name;this.mipaaRating = mipaaRating;this.terrible = 0;this.bad = 0;this.ok = 0;this.good = 0;this.great = 0;}//Adding the ratings for Terrible to Excellent ratingpublic void addRating(int rating) {if (rating >= 1 && rating <= 5) {switch (rating) {case 1:this.terrible++;break;case 2:this.bad++;break;case 3:this.ok++;break;case 4:this.good++;break;case 5:this.great++;break;}}}// Finds the average for every single rating when givenpublic double getAverage() {int total = this.terrible + this.bad + this.ok + this.good + this.great;if (total == 0) {return 0;} else {double sum = this.terrible + (2 * this.bad) + (3 * this.ok) + (4 * this.good) + (5 * this.great);double avg = sum / total;return Math.round(avg * 100.0) /…arrow_forward•Person Class: Person class has attributes: String name, address and int age. Write setperson() function to set values and getPerson() to Print attributes. Also write appropriate constructors. •Employee Class: Write another class Employee having attributes department and salary of type string and double. Write methods setEmployee(), getEmployee() and appropriate constructors for Employee class. •Student Class: •Write a class Student having attributes registration number and GPA of type string and float. Also write setStudent(), getStudent() methods and required constructors. Use the concept of inheritance to achieve the above functionality. Write a main() function to display the information of employee and student. • Note: Call the constructors/methods of parent class in child class where requiredarrow_forwardCode 16-1 /** This class has a recursive method. */ public class EndlessRecursion { public static void message() { System.out.println("This is a recursive method."); message(); } } Code 16.2 /** This class has a recursive method message, which displays a message n times. */ public class Recursive { public static void messge (int n) { if (n>0) { System.out.println (" This is a recursive method."); message(n-1); } } } Task #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 2. Run the program to confirm that the generated answer is correct. Modify the factorial method in the following ways: a. Add these lines above the first if statement: int temp; System.out.println("Method call -- " + "calculating " + "Factorial of: " + n); Copyright © 2019 Pearson Education, Inc., Hoboken NJ b. Remove this line in the recursive section at the end of the method: return…arrow_forward
- java programming I have a Java program with a restaurant/store menu. Can you please edit my program when displaying the physical receipt? I would like the physical receipt to export as text file, not print the console's order receipt. import java.util.*; public class Restaurant2 { publicstaticvoidmain(String[] args) { // Define menu items and pricesString[] menuItems= {"Apple", "Orange", "Pear", "Banana", "Kiwi", "Strawberry", "Grape", "Watermelon", "Cantaloupe", "Mango"};double[] menuPrices= {1.99, 2.99, 3.99, 4.99, 5.99, 6.99, 7.99, 8.99, 9.99, 10.99};StringusersName; // The user's name, as entered by the user.ArrayList<String> arr = new ArrayList<>(); // Define scanner objectScanner input=new Scanner(System.in); // Welcome messageSystem.out.println("Welcome to AppleStoreRecreation, what is your name:");usersName = input.nextLine(); System.out.println("Hello, "+ usersName +", my name is Patrick nice to meet you!, May I Take Your Order?"); // Display menu items and…arrow_forward•Person Class: Person class has attributes: String name, address and int age. Write setperson() function to set values and getPerson() to Print attributes. Also write appropriate constructors.•Employee Class: Write another class Employee having attributes department and salary of type string and double. Write methods setEmployee(), getEmployee() and appropriate constructors for Employee class.•Student Class:•Write a class Student having attributes registration number and GPA of type string and float. Also write setStudent(), getStudent() methods and required constructors. Use the concept of inheritance to achieve the above functionality. Write a main() function to display the information of employee and student.• Note: Call the constructors/methods of parent class in child class where required in java codearrow_forwardpackage lab1; /** * A utility class containing several recursive methods * * <pre> * * For all methods in this API, you are forbidden to use any loops, * String or List based methods such as "contains", or methods that use regular expressions * </pre> * * */ public final class Lab1 { /** * This is empty by design, Lab class cannot be instantiated */ privateLab1(){ // empty by design } /** * Returns the product of a consecutive set of numbers from <code> start </code> * to <code> end </code>. * * @param start is an integer number * @param end is an integer number * @return the product of start * (start + 1) * ..*. + end * @pre. * <code> start </code> and <code> end </code> are small enough to let * this method return an int. This means the return value at most * requires 4 bytes and no overflow would happen. */ publicstaticintproduct(ints,inte){ if(s==e){ returne; }else{ returns*product(s+1,e); } }…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education