Writing Classes

class Food {
    //attributes
    private String cuisine;
    private String name;
    private int mealsConsumed;
//instance variables

    //constructor
    Food(String cuisine, String name, int mealsConsumed)
    {
        this.cuisine = cuisine;
        this.name = name;
        this.mealsConsumed = mealsConsumed;
    }
    public String getName(){
        return name;
    }
}

public class Foodmain {
    public static void main(String[] args) {
        Food newFood = new Food("Indian", "Dal", 20);
        System.out.println(newFood.getName());
        
    }
}
Foodmain.main(null)
Dal
import java.util.Scanner; //allows scanners to be in program

public class averages {
  public static void main(String[] args) {
    
}}
Scanner in = new Scanner(System.in); //creates a scanner

System.out.print("Input 1st number: "); //asks user for input
int num1 = in.nextInt(); //saves number and goes to next int
System.out.println(num1);
System.out.print("Input 2nd number: "); //same as above
int num2 = in.nextInt();
System.out.println(num2);
System.out.println("The average of those numbers are " + (num1 + num2)/(2));

//prints out
//num you want to divide is however many numbers you want to average out

averages.main(null)
Input 1st number: 2
Input 2nd number: 20
The average of those numbers are 11

Constructors

  • no return b/c not directly called
  • called for object creation
  • initialize and set values for objects
public class Main {//main class
    int modelYear;
    String modelName;//attributes
  
    public Main(int year, String name) {//constructor
      modelYear = year;
      modelName = name;
    }
  
    public static void main(String[] args) {
      Main myCar = new Main(1969, "Mustang");//objects
      System.out.println(myCar.modelYear + " " + myCar.modelName);
    }
  }
  
// Outputs 1969 Mustang
Main.main(null);
1969 Mustang

Public, Private, Protected; Access Modifiers

  • private; only accessed within class
  • protected; within/outside package, accessible only through child class
  • public - open access
//public


//Exercise 1: 
//Write a program to reverse the words in a sentence
//example: the strawberry cake
//becomes: cake strawberry the

//hints: use scanner to take in words
//use split to break apart words to reverse them

import java.util.*; //import scanner
public class Main {
  public static String reverse_str_order(String input_sentence) { //
    if (input_sentence == null) {
      throw new IllegalArgumentException("Null"); //no empty strings
    }
    StringBuilder stringBuilder = new StringBuilder();
    String[] words = input_sentence.split(" ");
    for (int i = words.length - 1; i >= 0; i--) {
      stringBuilder.append(words[i]); //place words in order
      if (i != 0) { //keep going until no more words are left
        stringBuilder.append(" ");
      }
    }
    return stringBuilder.toString();
  }
}
public static void main(String[] args) { //prepares for scanning
  Scanner scanner = new Scanner(System.in);
  System.out.print("Input string: "); //asks for user input
  String input = scanner.nextLine(); //takes in next line
  System.out.println("\nResult: " + reverse_str_word(input));
}
//private


//Exercise 1: 
//Write a program to reverse the words in a sentence
//example: the strawberry cake
//becomes: cake strawberry the

//hints: use scanner to take in words
//use split to break apart words to reverse them

import java.util.*; //import scanner
public class Main {
  private static String reverse_str_order(String input_sentence) { //
    if (input_sentence == null) {
      throw new IllegalArgumentException("Null"); //no empty strings
    }
    StringBuilder stringBuilder = new StringBuilder();
    String[] words = input_sentence.split(" ");
    for (int i = words.length - 1; i >= 0; i--) {
      stringBuilder.append(words[i]); //place words in order
      if (i != 0) { //keep going until no more words are left
        stringBuilder.append(" ");
      }
    }
    return stringBuilder.toString();
  }
}
public static void main(String[] args) { //prepares for scanning
  Scanner scanner = new Scanner(System.in);
  System.out.print("Input string: "); //asks for user input
  String input = scanner.nextLine(); //takes in next line
  System.out.println("\nResult: " + reverse_str_word(input));
}
//protected


//Exercise 1: 
//Write a program to reverse the words in a sentence
//example: the strawberry cake
//becomes: cake strawberry the

//hints: use scanner to take in words
//use split to break apart words to reverse them

import java.util.*; //import scanner
public class Main {
  protected static String reverse_str_order(String input_sentence) { //
    if (input_sentence == null) {
      throw new IllegalArgumentException("Null"); //no empty strings
    }
    StringBuilder stringBuilder = new StringBuilder();
    String[] words = input_sentence.split(" ");
    for (int i = words.length - 1; i >= 0; i--) {
      stringBuilder.append(words[i]); //place words in order
      if (i != 0) { //keep going until no more words are left
        stringBuilder.append(" ");
      }
    }
    return stringBuilder.toString();
  }
}
public static void main(String[] args) { //prepares for scanning
  Scanner scanner = new Scanner(System.in);
  System.out.print("Input string: "); //asks for user input
  String input = scanner.nextLine(); //takes in next line
  System.out.println("\nResult: " + reverse_str_word(input));
}

this Keyword

  • current object in method/constructor
  • eliminates confusion b/w attributes/parameters w/ the same name
//ex
public class Main {
    int x;
  
    // Constructor with a parameter
    public Main(int x) {
      this.x = x;
    }
  //this used to 
    // Call the constructor
    public static void main(String[] args) {
      Main myObj = new Main(5);
      System.out.println("Value of x = " + myObj.x);
    }
  }

Inheritance, extends

  • extends: extends a class, inherits information from another class
  • subclass; class inherits from another class
  • superclass; parent class being inherited from

Big O notation for Hash map, Binary Search, Single loop, Nested Loop

  • language for runtime length
  • related to efficiency

  • hashmap: single operation to obtain position of a searched element

    • O(1)
  • binary search

    • O(1)
    • eliminates half the elements each time
  • single loop

    • O(n)
    • The next loop executes N times, if we assume the statement inside the loop is O(1), then the total time for the loop is N*O(1), which equals O(N) also known as linear time
  • nested loop

    • O(N*N)
    • the first loop may execute N times, but for how many first loop executes, the inner loop executes N times

Implementation

//2021 FRQ 1A 3A

//unit 5
//1a
public int scoreGuess( String guess ) {
    int val = 0;
    int len = guess.length();
    for( int i = 0; i <= secret.length()-len; i+=1){
    String ck = secret.substring( i, i+len );
    
    if( ck.equals(guess) ){
        val++;
    }
    }
    return val*len*len;
    }
//3a
    public void addMembers(
    String[] names, int gradYear ){
        
    for( String n : names ){
    memberList.add(new MemberInfo( n, gradYear, true) );
    }
    }