LynxPlay

Some secret homework #1

Oct 31st, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. public class MyClass {
  2.  
  3.     public static void main(String[] args) {
  4.         User michael = new User("Michael King", "supersecret");
  5.         User pande = new User("Pandetta Bell", "Pandetta Bell");
  6.  
  7.         michael.login();
  8.  
  9.         MessengerClient client1 = new FacebookMessenger(michael);
  10.         MessengerClient client2 = new TwitterMessenger(pande);
  11.  
  12.         client1.publish("Good morning login want my beauxbatons");
  13.         client2.publish("Yeah no. Wont happen");
  14.     }
  15.  
  16. }
  17.  
  18.  
  19. public class User {
  20.  
  21.     private String username, password;
  22.     private boolean loggedIn = false;
  23.  
  24.     public User(String username, String password) {
  25.         this.username = username;
  26.         this.password = password;
  27.     }
  28.  
  29.     public String getUsername() {
  30.         return username;
  31.     }
  32.  
  33.     public String getPassword() {
  34.         return password;
  35.     }
  36.  
  37.     public boolean isLoggedIn() {
  38.         return loggedIn;
  39.     }
  40.  
  41.     public void login() {
  42.         this.loggedIn = true;
  43.     }
  44. }
  45.  
  46.  
  47. public abstract class MessengerClient {
  48.  
  49.     private User user;
  50.  
  51.     public MessengerClient(User user) {
  52.         this.user = user;
  53.     }
  54.  
  55.     public User getUser() {
  56.         return user;
  57.     }
  58.  
  59.     public abstract void publish(String message);
  60.  
  61. }
  62.  
  63.  
  64. public class TwitterMessenger extends MessengerClient {
  65.  
  66.     public TwitterMessenger(User user) {
  67.         super(user);
  68.     }
  69.  
  70.     @Override
  71.     public void publish(String message) {
  72.         if(!getUser().isLoggedIn()) return;
  73.  
  74.         System.out.println("Twitter >> " + getUser().getUsername() + " tweeted " + message);
  75.     }
  76. }
  77.  
  78. public class FacebookMessenger extends MessengerClient {
  79.  
  80.     public FacebookMessenger(User user) {
  81.         super(user);
  82.     }
  83.  
  84.     @Override
  85.     public void publish(String message) {
  86.         if(!getUser().isLoggedIn()) return;
  87.  
  88.         System.out.println("Facebook>> " + getUser().getUsername() + " posted something new to his timeline: " + message);
  89.     }
  90. }
  91.  
  92.  
  93. Homework. Topic: Abstract super classes
  94.  
  95. 1. What does this program print on execute (this is a tricky one)
  96.  
  97. 2. What does the keyword abstract mean (google wooo)
  98.  
  99. 3. Both client1 and client2 are stored in a Variable of the type MessengerClient (compare ln 11 and 12) They still produce different prefixes. How come so ?
  100.  
  101. 4. What is the java keyword "super" (google some more o.O its the last time i swear)
  102.  
  103. 5. Describe the User type as well as each of its method in the context. (What would it be used for. What does it represent? What do the methods do)
  104.  
  105. 6. Write another MessengerClient, this time for Instagramm.
  106.  
  107. 7. What does line 7 do in this program. What are its effects
  108.  
  109. 8. What would happen if we delete lines 7 What would the program print
Add Comment
Please, Sign In to add comment