Java OOP - Generic Classes - Q1
@MoosesValley - Software development, SciFi, Doggies, Animal Rescues, Quad Bikes, and more
A recording of me working on a Generic Classes question: Q1.
Q1. Generic Classes
The TourTest class, mentioned below, produces the following output:
Name = Sydney, price = $250 Name = National Parks, price = $125
public class Tour { String name; int price;
public Tour( String name, int price) { this.name = name; this.price = price; }
public String getName() { return name; }
public void setName( String name) { this.name = name; }
public int getPrice() { return price; }
public void setPrice( int price) { this.price = price; }
@Override public String toString() { return "Name = " + name + ", price = $" + price + '\n'; }
}// end of class Tour
public class TourTest { public static void main( String[] args) { Tour tour1 = new Tour("Sydney", 250); Tour tour2 = new Tour("National Parks", 125);
System.out.println( tour1);
System.out.println( tour2);
} } // end of class TourTest1
You have to rewrite the above mentioned Tour class as a generic class, so that the following TourTest class can produce the output as mentioned below:
Name = Sydney, price = $250 Name = National Parks, price = $110.75
// Note: less than and greater than signs removed - they are NOT // allowed in descriptions in YouTube would. public class TourTest { public static void main( String [] args) { Tour Integer tour1 = new Tour Integer ("Sydney", 250); Tour Double tour2 = new Tour Double ("National Parks", 110.75);
System.out.println( tour1);
System.out.println( tour2);
} }
Moose's Software Valley - Established July, 1996. https://rebrand.ly/MoosesSoftware ... https://www.youtube.com/watch?v=PpMpPpyi1NI
16628144 Bytes