Explanation of Solution
Output of the code:
Create a main class Maryland that extends the State class and it includes printMe() to display the respective header in console.
// Create a class Maryland
public class Maryland extends State
{
// Null constructor
Maryland( )
{
}
// Create a method
public void printMe( )
{
System.out.println("Read it.");
}
Create a main() function and create the objects for the class using new keyword and call the method printMe() to display the respective header.
// Main function
public static void main(String[ ] args)
{
// Create the objects for classes
Region east = new State( );
State md = new Maryland( );
Object obj = new Place( );
Place usa = new Region( );
/* Call the method printMe() using state class object.*/
md.printMe( );
// Call the method printMe() using object
east.printMe( );
// Call the method printMe() in Place class
((Place) obj).printMe( );
//Assign the state class object to object class
obj = md;
// Call the method printMe() in maryland class
((Maryland) obj).printMe( );
//Assign the place class object to object class
obj = usa;
// Call the method printMe() in place class
((Place) obj).printMe( );
//Assign the state class object to place class
usa = md;
// Call the method printMe() in place class
((Place) usa).printMe( );
}
}
Create a class State that extends the class Region and includes a method printMe() to display the header.
// Create a class that extends the Region
class State extends Region
{
//Null constructor
State( )
{
}
//Create a method
public void printMe( )
{
//Display the header
System.out.println("Ship it.");
}
}
Create a class Region that extends the class Place and includes a method printMe() to display the header...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Data Structures and Algorithms in Java
- class Param3 { public int x; private void increase(int p) { x = x*p; } public void calculateX(int y) { increase(y); } public int getX() { return x; } } // in another class Param3 q3 = new Param3(); q3.x = 5; q3.calculateX(7); System.out.println(q3.getX()); what would be the answer for the last two lines ? also above were x = x*p do both x in here are the fields? wouldn't that be cnofusing?arrow_forwardpublic class Main{ public static void main(String args[]) { Main m = new Main(); m.go(); } public void go() { Customer c = new Customer("Tom", "Smith"); Order o = new Order(c); o.addItem(new Item("Greeting Card", 1.50, 1)); o.addItem(new Item("Baseball Glove", 54.00, 1)); o.addItem(new Item("Notebook", 2.50, 3)); System.out.println(o); }}arrow_forwardFix this program import java.util.Scanner; public class main { public static void WeShipItBad(String[] args) { int OVERNIGHT_CHARGE = 5; int TWO_DAY_CHARGE = 2; int ECONOMY_CHARGE = 1; Scanner console = new Scanner(System.in); // Scanner object to pass around. // Get item description. String itemDescription = getItemDescription(console); if (itemDescription.equals("")) { System.out.println("Invalid description."); } shipWeight = getShipWeight(scnr); if (shipWeight <= 0.0) { System.out.println(); System.out.println("Invalid weight."); } char shipMethod = getShipMethod(council); shipMethod = Character.toUpperCase(shipMethod); double shipCost = calculateShip(shipWeight, shipMethod); displayResults(itemDescription, shipCost, shipMethod, shipWeight); } public static void getItemDescription(Scanner console) { System.out.println("Enter item description:"); String description =…arrow_forward
- public class MyGenClass { private T1 name; private T2 stulD; private T3 CGPA; public void setStuData( ){ l parameters name = n; stulD = d: CGPA = g; public void printAsString() { System.out.printin( '): # print all Information as string public static void main(String args||) { Il Create an object stu by parameters String, Integer, Double II Call setStuData() with any values you like Il Call printAsString()arrow_forwardJAVA Language Caesar Shift Question: Modify the Caesar class so that it will allow various sized shifts to be used, instead of just a shift of size 3. (Hint: Use an instance variable in the Caesar class to represent the shift, add a constructor to set it, and change the encode method to use it.) import java.util.*; public class TestCipher { public static void main(String[] args) { int shift = 7; Caesar caesar = new Caesar(); String text = "hello world"; String encryptTxt = caesar.encrypt(text); System.out.println(text + " encrypted with shift " + shift + " is " + encryptTxt); } } abstract class Cipher { public String encrypt(String s) { StringBuffer result = new StringBuffer(""); // Use a StringBuffer StringTokenizer words = new StringTokenizer(s); // Break s into its words while (words.hasMoreTokens()) { // For each word in s…arrow_forwardQuestion 13 What is outpout? public class Vehicle { public void drive(){ System.out.println("Driving vehicle"); } } public class Plane extends Vehicle { @Override public void drive(){ System.out.println("Flying plane"); } public static void main(String args[]) { Vehicle myVehicle= = new Plane(); myVehicle.drive(); } Driving vehicle syntax error Flying plane Driving vehicle Flying plane }arrow_forward
- 1- public class Con{ public static void main(String[] args) int[] ar=(2,5,7,9}; int i=2; final int j=1; 6. for (int x:ar) 8- { 9 if(ar[i]arrow_forwardclass TenNums {private: int *p; public: TenNums() { p = new int[10]; for (int i = 0; i < 10; i++) p[i] = i; } void display() { for (int i = 0; i < 10; i++) cout << p[i] << " "; }};int main() { TenNums a; a.display(); TenNums b = a; b.display(); return 0;} Write a copy constructor for the above class TenNums. Make sure it performs deep copy. Group of answer choices TenNums(const TenNums& b) { p = b.p;} TenNums(const TenNums& b) { p = new int[10]; for (int i = 0; i < 10; i++) p[i] = b.p[i];} TenNums(const TenNums b) { p = new int[10]; for (int i = 0; i < 10; i++) p[i] = b.p[i];} TenNums(const TenNums b) { p = b.p;}arrow_forwardclass TenNums {private: int *p; public: TenNums() { p = new int[10]; for (int i = 0; i < 10; i++) p[i] = i; } void display() { for (int i = 0; i < 10; i++) cout << p[i] << " "; }};int main() { TenNums a; a.display(); TenNums b = a; b.display(); return 0;} Continuing from Question 4, let's say I added the following overloaded operator method to the class. Which statement will invoke this method? TenNums TenNums::operator+(const TenNums& b) { TenNums o; for (int i = 0; i < 10; i++) o.p[i] = p[i] + b.p [i]; return o;} Group of answer choices TenNums a; TenNums b = a; TenNums c = a + b; TenNums c = a.add(b);arrow_forwardpublic class MyGenClass { private T1 name; private T2 stulD; private T3 CGPA; public void setStuData( ){ I/ parameters name = n; stulD = d: CGPA = g; } public void printAsString() { System.out.println( } ); I/ print all information as string public static void main(String args||) { Il Create an object stu by parameters String, Integer, Double Il Call setStuData() with any values you like /l Call printAsString() } }arrow_forwardclass class { public static void main(String args[]) { int a =5; int b =10; first: { second: { third: { if(a == b >>1) break second; } System.out.println(a); } System.out.println(b); } } } Give result for the code Java Try to do ASAParrow_forwardFor the code below: public class College extends Student { public static void main(String[ ] args) { new College( ); } public College ( ) { this(“First semester"); System.out.println( "Third year" ); } public College (string G ) { System.out.println(G); } } class Student extends S_info { public Student ( ) { System.out.println("Student No. 123"); } public Student (String S) { System.out.println(S); } } class S_info{ public S_info ( ) { System.out.println("student age: 21"); } } 1- Draw the diagram for the constructor chaining. 2- Put the values for the strings G and S. 3- Trace the output for the entire code.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- 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