All in JAVA B) Polymorphism: Implement the Speaker interface: public interface S
ID: 3665483 • Letter: A
Question
All in JAVA
B)
Polymorphism:
Implement the Speaker interface: public interface Speaker { public void speak(); public void announce(String str); } Then, create three classes that implement Speaker in various ways: • SpeakerOfTheHouse: speak method prints "I am Speaker of the House.", and the announce method takes the name of a bill in the form of a string and prints it in the sentence "The [bill] has passed!" • SportsAnnouncer: speak method prints "Goal!", and the announce method takes the name of a team in the form of a string and prints the sentence "The [team] have scored a goal!" • Actor: speak method prints "I've been nominated for three Academy Awards.", and the announce method takes the name of a movie in the form of a string and prints the sentence, "I'm currently staring in [movie]." Create a driver class , and in the main method , prompt the user to enter three Strings -- the name of a bill, the name of a sports team, and the name of a movie. Then, create an object from each of the classes described above and call the speak and announce methods of each object , using the strings provided by the user.
(c)
Using Classes in java Library (1) Write a program • to create a 2x10 two dimensional integer array . • use Math.random() from java.util.Random to generate 20 integer numbers from 100 to 1000 • store each random number in the two dimension array. • At the end, print the array. Then switch numbers between two rows then print the array again. (2) Write a program to accept input string from keyboard and uses substring method to revert the string and print it out.
Explanation / Answer
Polymorphism: (B)
public interface Speaker
{
public void speak();
public void announce(String str);
}
public class SpeakerOfTheHouse implements Speaker
{
public :
String bill;
public void speak()
{
System.out.println("I am Speaker of the House.");
}
public void announce(String bill)
{
System.out.println("The " + bill + " has passed!");
}
}
public class SportsAnnouncer implements Speaker
{
public :
String team;
public void speak()
{
System.out.println("Goal!");
}
public void announce(String team)
{
System.out.println("The " + team + " have scored a goal!");
}
}
public class Actor implemets Speaker
{
public :
String movie;
public void speak()
{
System.out.println("I've been nominated for three Academy Awards.");
}
public void announce(String movie)
{
System.out.println("I'm currently staring in " + movie + ".");
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter three Strings - *Name of a bill, *Name of a sports team, *Name of a movie");
String bill = in.next();
String team = in.next();
String movie = in.next();
SpeakerOfTheHouse s = new SpeakerOfTheHouse(bill);
SportsAnnouncer t = new SportsAnnouncer(team);
Actor a = new Actor(movie);
s.speak(); s.announce(bill);
t.speak(); t.announce(team);
a.speak(); a.announce(movie);
}
(C).
(1) Write a program
• to create a 2x10 two dimensional integer array .
• use Math.random() from java.util.Random to generate 20 integer numbers from 100 to 1000
• store each random number in the two dimension array.
• At the end, print the array.
Then switch numbers between two rows then print the array again.
-->
public static void main(String[] args)
{
int[][] arr = new int[2][10];
int i,j;
Random rGen = new Random();
/* Fill array with random numbers */
for(i=0; i<2; i++) for(j=0; j<10; j++) { arr[i][j] = ((rGen.nextInt()%(1000-100+1)) + 100); }
/* Print 2-D array */
for(i=0; i<2; i++){ for(j=0; j<10; j++){ System.out.print(arr[i][j]);} System.out.print(" "); }
/* Swap two rows -- r1 & r2 -- say 0 & 1 */
int r1=0, r2=1;
for (i=0; i<2; ++i){
int temp;
temp = arr[r2][i];
arr[r2][i] = arr[r1][i];
arr[r1][i] = temp;
}
/* Print 2-D array after swapping the first two rows */
for(i=0; i<2; i++){ for(j=0; j<10; j++){ System.out.print(arr[i][j]);} System.out.print(" "); }
}
(2) Write a program to accept input string from keyboard and uses substring method to revert the string and print it out.
-->
public static void main(String[] args)
{
String inp_string = null;
Scanner in = new Scanner(System.in);
inp_string = in.next();
String revString = null;
int len = inp_string.length();
for(int i =0;i<len;i++)
{
String subChar = inp_string.substring(inp_string.length()-1-i, inp_string.length()-i);
revString += subChar;
System.out.println(revString);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.