public class MadLib {

    String[] people = {"Meena", "Bob", "John", "Joe"};
    String[] places = {"house", "school", "mall", "restaurant"};
    String[] adjectives = {"slimy", "depressed", "pink", "horrible"};

    // using Math.random and type casting to generate a random integer 
    int num = (int)(Math.random() * 4);

    private String name = people[num];
    private String place = places[num];
    private String adjective = adjectives[num];

    public static void main(String[] args) {
    
        MadLib lib = new MadLib();
        System.out.println("once upon a time, there was a person named " + lib.name + " who went to a " + lib.adjective + " " + lib.place + ".");
}
}
MadLib.main(null);
once upon a time, there was a person named John who went to a pink mall.

The MadLib uses arrays and data types such as String and int to generate a random sentence.

import java.util.ArrayList;

public class MadLibWrapper {

    ArrayList<String> people = new ArrayList<String>();
    
    ArrayList<String> places = new ArrayList<String>();
    
    ArrayList<String> adjectives = new ArrayList<String>();
    int num;
    String name;
    String place;
    String adjective;
    
    public MadLibWrapper(){
        people.add("John");
        people.add("Bob");
        places.add("house");
        places.add("mall");
        adjectives.add("slimy");
        adjectives.add("depressed");
         // using Math.random and type casting to generate a random integer 
    
     name = people.get(num);
      place = places.get(num);
      adjective = adjectives.get(num);
        
    }
   
   

    public static void main(String[] args) {
    
        MadLibWrapper lib = new MadLibWrapper();
        System.out.println("once upon a time, there was a person named " + lib.name + " who went to a " + lib.adjective + " " + lib.place + ".");
}
}
MadLibWrapper.main(null);
once upon a time, there was a person named John who went to a slimy house.

The MadLibWrapper class does the same as the MadLib class except it uses ArrayList instead of arrays. Each element in the ArrayList is of the wrapper class rather an a primitive type.

Key Takeaways from Lesson

Methods and Control Structures and Data Types from ChatGPT

  • Methods are functions that perform specific tasks or operations within a program.
  • Control structures enable decision-making and repetition in programming.
  • Examples of control structures include if-else statements, loops, and switch statements.
  • Data types define the type of data that can be stored in a variable.
  • Common data types include integers, floats, strings, and booleans.
  • Proper use of methods, control structures, and data types is essential for writing efficient and effective code.

Mr M's Code

  • DoNothingByValue: using parameters in java, local and global variables
  • IntByReference and IntByValue: difference between local and global variables
  • Menu code: reviewed as treo

FRQ 1 Question

2017 APCSA FRQ

This question involves identifying and processing the digits of a non-negative integer. The declaration of the Digits class is shown below. You will write the constructor and one method for the Digits class.

a) Write the constructor for the Digits class. The constructor initializes and fills digitList with the digits from the non-negative integer num. The elements in digitList must be Integer objects representing single digits, and appear in the same order as the digits in num. Each of the following examples shows the declaration of a Digits object and the contents of digitList as initialized by the constructor.

  • Digits d1 = new Digits(15704);
  • digitList: 1, 5, 7, 0, 4

b) Write the Digits method isStrictlyIncreasing. The method returns true if the elements of digitList appear in strictly increasing order; otherwise, it returns false. A list is considered strictly increasing if each element after the first is greater than (but not equal to) the preceding element. The following table shows the results of several calls to isStrictlyIncreasing

FRQ 1 Treo Solution

(Outline for the Digits class given, added code - constructor for Digits and isStrictlyIncreasing method)

public class Digits 

{ 

/** The list of digits from the number used to construct this object. 

* The digits appear in the list in the same order in which they appear in the original number.
 */ 

    private ArrayList<Integer> digitList; 

    /** Constructs a Digits object that represents num.
     * Precondition: num >= 0
    */ 

    public Digits(int num) { 
        // create the array list of integers 
        digitList = new ArrayList<Integer> ();
        // take what is passed into the constructor and convert it to a string
        String number = String.valueOf(num);
        // Loop through the string and add each character converted integer into the integer array list
        for (int i = 0; i < number.length(); i++) {
            digitList.add(Integer.parseInt(String.valueOf(number.charAt(i))));
        }
        // print out the array list
        System.out.println(digitList);
     }

    /** Returns true if the digits in this Digits object are in strictly increasing order;
     * false otherwise.
     */ 

    public boolean isStrictlyIncreasing() 
    {   // loop through the entire array list of integers
         for (int i = 0; i < digitList.size(); i++) {
            // if the term is not less than (so greater than or equal to) the next term, return false
            if (digitList.get(i) >= digitList.get(i+1)) {
                return false;
            }
         }
         // if every number is greater than the next and so the prev if statement is false, return true
         return true;
    } 

}

Testing the code

private ArrayList<Integer> digitList; 
digitList = new ArrayList<Integer> ();

//expect to get an out put of a list of 12345 and true since the numbers are increasing
String number = String.valueOf(12345);

for (int i = 0; i < number.length(); i++) {
    digitList.add(Integer.parseInt(String.valueOf(number.charAt(i))));
}
System.out.println(digitList); 

for (int i = 0; i < digitList.size() - 1; i++) {
    if (digitList.get(i) >= digitList.get(i+1)) {
        System.out.println("false"); 
        //note: would be a return statement so would exit out of the loop
    }
 }
 System.out.println("true");
Queue<String> queue = new LinkedList<>();  // Queue interface uses LL implementation
queue.add("John");
queue.add("Jane");
queue.add("Bob");

Queue<String> queue = new LinkedList<>();  // Queue interface uses LL implementation
queue.add("John");
queue.add("Jane");
queue.add("Bob");
// Collections has a toArray convertion
Object[] arr = queue.toArray();

// Empty queue
System.out.println("Empty Queue");
while (queue.size() > 0) // Interate while size
    System.out.println(queue.remove());

// Iterate of array
System.out.println("Iterate over Array");
for (Object a : arr) // Type is Object from convertion
    System.out.println(a);
Empty Queue
John
Jane
Bob
Iterate over Array
John
Jane
Bob
/* This is wrapper class...
 Objective would be to push more functionality into this Class to enforce consistent definition
 */
//static applies to all the objects
public abstract class Generics {
	public final String masterType = "Generic";
	private String type;	// extender should define their data type

	// generic enumerated interface
	public interface KeyTypes {
		String name();
	}
	protected abstract KeyTypes getKey();  	// this method helps force usage of KeyTypes

	// getter
	public String getMasterType() {
		return masterType;
	}

	// getter
	public String getType() {
		return type;
	}

	// setter
	public void setType(String type) {
		this.type = type;
	}
	
	// this method is used to establish key order
	public abstract String toString();

	// static print method used by extended classes
	public static void print(Generics[] objs) {
		// print 'Object' properties
		System.out.println(objs.getClass() + " " + objs.length);

		// print 'Generics' properties
		if (objs.length > 0) {
			Generics obj = objs[0];	// Look at properties of 1st element
			System.out.println(
					obj.getMasterType() + ": " + 
					obj.getType() +
					" listed by " +
					obj.getKey());
		}

		// print "Generics: Objects'
		for(Object o : objs)	// observe that type is Opaque
			System.out.println(o);

		System.out.println();
	}
}
public class Alphabet extends Generics {
	// Class data
	public static KeyTypes key = KeyType.title;  // static initializer
	public static void setOrder(KeyTypes key) {Alphabet.key = key;}
	public enum KeyType implements KeyTypes {title, letter}
	private static final int size = 26;  // constant used in data initialization

	// Instance data
	private final char letter;
	
	/*
	 * single letter object
	 */
	public Alphabet(char letter)
	{
		this.setType("Alphabet");
		this.letter = letter;
	}

	/* 'Generics' requires getKey to help enforce KeyTypes usage */
	@Override
	protected KeyTypes getKey() { return Alphabet.key; }

	/* 'Generics' requires toString override
	 * toString provides data based off of Static Key setting
	 */
	@Override
	public String toString()
	{
		String output="";
		if (KeyType.letter.equals(this.getKey())) {
			output += this.letter;
		} else {
			output += super.getType() + ": " + this.letter;
		}
		return output;
	}

	// Test data initializer for upper case Alphabet
	public static Alphabet[] alphabetData()
	{
		Alphabet[] alphabet = new Alphabet[Alphabet.size];
		for (int i = 0; i < Alphabet.size; i++)
		{
			alphabet[i] = new Alphabet( (char)('A' + i) );
		} 	
		return alphabet;
	}
	
	/* 
	 * main to test Animal class
	 */
	public static void main(String[] args)
	{
		// Inheritance Hierarchy
		Alphabet[] objs = alphabetData();

		// print with title
		Alphabet.setOrder(KeyType.title);
		Alphabet.print(objs);

		// print letter only
		Alphabet.setOrder(KeyType.letter);
		Alphabet.print(objs);
	}
	
}
Alphabet.main(null);
class [LREPL.$JShell$25$Alphabet; 26
Generic: Alphabet listed by title
Alphabet: A
Alphabet: B
Alphabet: C
Alphabet: D
Alphabet: E
Alphabet: F
Alphabet: G
Alphabet: H
Alphabet: I
Alphabet: J
Alphabet: K
Alphabet: L
Alphabet: M
Alphabet: N
Alphabet: O
Alphabet: P
Alphabet: Q
Alphabet: R
Alphabet: S
Alphabet: T
Alphabet: U
Alphabet: V
Alphabet: W
Alphabet: X
Alphabet: Y
Alphabet: Z

class [LREPL.$JShell$25$Alphabet; 26
Generic: Alphabet listed by letter
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z