Activities
Professions.java
import java.util.*;
// Superclass
// Subclass:
// Subclass:
// Subclass:
// Subclass:
// Main class
public class Professions {
public static void main(String[] args) {
// create an Array of Professions
// Creating instances of each profession
// Adding objects to the Array
// Loop through Array of Professions using instanceof and calling methods
}
}
SquareDemo.java
// A class called Rectangle that has two instance variables, length and width,
// a constructor that initializes them, and a method called draw that uses nested loops
// to draw a length x width rectangle of stars.
class Rectangle {
private int length;
private int width;
// Constructor that initializes length and width
public Rectangle(int l, int w) {
length = l;
width = w;
}
// Method to draw a rectangle of stars based on length and width
public void draw() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
// Add an area method to compute the area of the rectangle
}
// Make the class Square below inherit from Rectangle
// Add a Square constructor with 1 argument for a side
// that calls Rectangle's constructor with 2 arguments using super
// Add another subclass called LongRectangle which inherits from Rectangle
// but has the additional condition that the length is always 2 x the width
public class SquareDemo {
public static void main(String[] args) {
// Create a Rectangle object and draw it
Rectangle r = new Rectangle(3, 5);
// System.out.println("Area of rectangle: " + r.area());
r.draw();
// Uncomment the objects to test the squares
// Square s1 = new Square(1);
// System.out.println("Area of square with side 1: " + s1.area());
// s1.draw();
// Square s2 = new Square(3);
// System.out.println("Area of square with side 3: " + s2.area());
// s2.draw();
// System.out.println();
// Uncomment the objects to test the rectangle
// LongRectangle lr = new LongRectangle(4);
// System.out.println("Area of long rectangle with length 4: " + lr.area());
// lr.draw();
}
}
SocialMediaDemo.java
class SocialMediaUser {
private String username;
private String platform;
// constructor
// introduce method
public void introduce() {
}
// Getter methods
public String getUsername() {
return username;
}
public String getPlatform() {
return platform;
}
}
class Influencer extends SocialMediaUser {
private String contentNiche;
private int followerCount;
// constructor
@Override
public void introduce() {
}
// Helper method to format follower count (e.g., 1500 -> 1.5K)
private String formatFollowerCount(int count) {
if (count >= 1000000) {
return String.format("%.1fM", count / 1000000.0);
} else if (count >= 1000) {
return String.format("%.1fK", count / 1000.0);
} else {
return String.valueOf(count);
}
}
// Helper method to add a catchy tagline based on content niche
private void addTagline() {
switch (contentNiche.toLowerCase()) {
case "gaming":
System.out.println("Let's get this win! #GamerLife");
break;
case "fashion":
System.out.println("Serving looks 24/7! #StyleIcon");
break;
case "music":
System.out.println("Dropping beats and making waves! #MusicLife");
break;
case "fitness":
System.out.println("No pain, no gain! #FitCheck");
break;
default:
System.out.println("Don't forget to like and subscribe! #ContentCreator");
}
}
}
public class SocialMediaDemo {
public static void main(String[] args) {
// Create a regular social media user
// Create some influencers with different niches
// Test the introduce methods
}
}
Customer.java
public class Customer {
private String name;
private String address;
public Customer(String n, String a) {
name = n;
address = a;
}
public String toString() {
return "Name: " + name + "\nAddress: " + address;
}
public static void main(String[] args) {
Customer c = new Customer("Fran Santiago", "123 Main St., Anytown, USA");
System.out.println(c);
// Uncomment these to test OnlineCustomer
// OnlineCustomer c2 = new OnlineCustomer("Jasper Smith",
// "456 High St., Anytown, USA", "[email protected]");
// System.out.println(c2);
}
}
// Complete the OnlineCustomer class to inherit from Customer
// It should have an email attribute,
// a constructor with 3 arguments (name, address, email) that uses the super
// constructor,
// and an overridden toString() method that calls the super toString() method
// and then prints "\nEmail:" and the email variable.
class OnlineCustomer {
}
Last updated