Question: 2. Given class DollApp.java below. Type and find the output. Write your answer the sheet provided. import java.util.Scanner; public class DollApp { public static void
2. Given class DollApp.java below. Type and find the output. Write your answer the sheet provided. import java.util.Scanner; public class DollApp { public static void main() { Scanner in = new Scanner(System.in); System.out.println("Creating a doll.."); Doll doll1 = new Doll(); doll1.pressMe(); doll1.setName("Rita"); doll1.pressMe(); System.out.print("Would you like to set name?Y/N:> "); char choice = in.next().charAt(); in.nextLine(); if((choice == 'Y*)||(choice == 'y')) { System.out.print("Enter the name: "); String name=in.nextLine(); doll1.setName (name); } doll1.pressMe(); } Output: 1. Create a class Doll with one attribute name of type String. Include set and get method for attribute name. Doll name: String + setName(String):void + getName():String +press Me():void Sample Code: public class Doll { private String name; public Doll() { name= "";} public Doll(String n) { setName(n); ) public void setName(String n) {name = n;} public void pressMe() { System.out.println("Hi I am " + name);}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
