CS102 - Object Oriented Programming


6/8

A special lecture from Rahul Dhodapkar.

Final Project - due 6/20 @ 11:59 pm

To submit this project, create a github repository and properly upload your files.

Your job to is to create a text-based adventure game. Your game will be the journey of a company man’s transformation to a Buddhist Monk. The player starts with a certain amount of money, and must earn 100 Zen points by doing good deeds. The goal is to become a master monk before you run out of money.

To see how the game should work, download this file. Open a powershell window from the folder containing that file, and run the file with java -jar Game.jar.

The commands are ordered by difficulty. “Meditate” is easiest, while “Purchase Temple” is the most difficult. If you do try to buy temples, start with only “Small Temple” then add “Big Temples”. Notice that all temples have names, but the Big Temple and Small Temple each do something different (hint: use extends here).

Your grade is broken down as follows:

Since this is a large project, you may want to download a more advanced editor. One option is Netbeans.


6/1

Introducing Inheritance

See the official description from Java on inheritance. This will allow use to use the extends keyword.


5/25

call-by-value vs call-by-reference

homework

Discover on your own if java call-by-value vs call-by-reference? No need to turn this in on LMS. Just bring your discovery to class.


5/23

I’m sick :( Make up class at the end of the semester.


5/18

Festival - great job on all your performances and activities.


5/16

In class

Review of midterm

See the grades

Homework

Make a zoo program

The user can add animals to the zoo by providing a name and sound. The new animals will be added to an array of animals in the zoo. You will need to write a function that will print the sound of all the animals.

Upload your code to Github, and submit a link on the lms. The link should look as follows: github.com/username/repository_name

To use github, follow this video.

You can use this code as a template.

//In Main.java
public static void main(String[] args){
  //make animals and add them to an array
  all_sounds(my_zoo);
}

public static void all_sounds(Animal[] zoo){
  // output...
  // The lion goes roar
  // The cat goes meow

}
'''

'''
//In Animal.java

public Animal(String name, String sound)


5/11

In class

Investor and stock program. static vs non-static functions.

what is the difference between the two buy functions below.

//in Investor.java
Stock apple = new Stock("APPL",127.98);
double p1 = apple.buy(5);
double p2 = Stock.buy(apple,5);
//in Stock.java
public double buy(int n){...}
public static double buy(Stock s, int n){...}

Homework

Read the ‘static 변수’ and ‘static method’ sections. You do not need to read about the singleton pattern. https://wikidocs.net/228

If you do not understand the above reading, also read this http://rockdrumy.tistory.com/214 for more examples.


5/09

In class

Investor and stock program. Multiple instances of an object.


5/04

In class

More on objects


5/02

In class

Make a program to grade a class. We will need two files…

The student object has a list of grades, but we need be careful how we edit them. For this, we introduce the private keyword and object functions.

We learned a few vocabulary words for functions:

Homework

Work on the Midterm - due May 8, 11:59pm


4/27

In class

Arrays will let us do more advanced operations. We have already seen some of these concepts in fact. They are similar to strings.

arrayimg

class ArrayDemo {
  public static void main(String[] args) {
    // declares an array of integers
    int[] myArray;

    // allocates memory for 2 integers
    myArray = new int[2];

    myArray[0] = 100;
    myArray[1] = 200;
    myArray[1] = myArray[0];

    System.out.println("Element at index 0: " + anArray[0]);
    System.out.println("Element at index 1: " + anArray[1]);
  }
}

[1,2,3,4,2] -> 4
[-5,7,0,3] -> 7
[1,2,3,4,2] -> 12
[-5,7,0,3] -> 5

Midterm Project - due May 8, 11:59pm


4/25

Use the Player field name in the new methods we have.


4/20

Review of the refactoring homework. How do we use methods for organization?

Intro to objects. Started writing the Player object.

How to use a constructor method.

//In Main.java
Player p1 = new Player("Mark");

//In Player.java
String name;

public Player(name){
 this.name = name;
}

4/18

In class

We reviewed the ceaser shift cipher homework. Well done everyone.

We also started using functions in a more useful way

Homework

Update your blackjack program so the main method looks like this…

public class Blackjack {
  public static void main(String[] args) {

    int human_total = play_human();
    int computer_total = play_computer();
    calculate_winner(human_total,computer_total);

  }

4/13

No class, go vote.


4/11

In class

Ceasear Cipher

ceaser

Quantum Crytography with Chris Klumpp You can view the slides

Homework

Write a java program for the ceasear cipher. Here is some code to help.


public class Cipher {
  public static void main(String[] args) {

    String input = "The quick brown fox Jumped over the lazy Dog";

    System.out.println (input.charAt(2));
    System.out.println( input.length());
    System.out.println((char)97);
    System.out.println((int)'a');
    System.out.println((char)('a'+1));

  }
}


4/07

In class

Review of Blackjack. We will look at every single person’s code and fix it.

Homework

None.


4/05

In class

How to be successful on your own

What is scope and how is it related to functions?

Homework

None - lucky you.


3/30

In class

Math.random() and type-casting with int(3.7) to build a card game.

import java.util.Scanner;
public class BlackJack{

  public static void main(String[] args){   
    //Don't change this line
    Scanner in = new Scanner(System.in);

    int human_card1 = (int)(Math.random()*11)+1;
    int human_card2 = (int)(Math.random()*11)+1;
    int human_total = human_card1 + human_card2;
    System.out.println("Human player got");
    System.out.println(human_card1+" and "+human_card2);

    System.out.println("Do you want another card (Y/N)");
    String s = in.nextLine();
    if(s.equals("Y")){
        human_total = human_total + (int)(Math.random()*11)+1;
        System.out.println("new human_total "+ human_total);

    }

    int computer_card1 = (int)(Math.random()*11)+1;
    int computer_card2 = (int)(Math.random()*11)+1;
    int computer_total = computer_card1 + computer_card2;
    System.out.println("Computer player got");
    System.out.println(computer_card1+" and "+computer_card2);

    if(human_total<=21 && computer_total<human_total){
      System.out.println("Human Wins");
    }
    else {
      System.out.println("Computer Wins");
    }

  }
}

Homework

(due 4/4) Add a loop to our version of blackjack. The user can ask for more than one more card. Keep asking until the user says “N”. HINT You might need to use a break; or a while() . Here is an example of how the program could run.

Human player got
3 and 1
Do you want another card (Y/N)
>>> Y
new human_total 9
Do you want another card (Y/N)
>>> Y
new human_total 18
Do you want another card (Y/N)
>>> N
Computer player got
10 and 3
Human Wins

3/28

In class

We introduced recursion.


3/23

In class

We introduced functions in java and compared them to python functions.

Homework

(due 3/28) Loops and Functions worksheet.


3/21

In class

We started with drjava to program on the local computer.

Homework

Install drjava and try it out.


3/16

In class

We reviewed if statements.

Homework

Java on codecademy.


3/14

In class

We started java syntax and compared it to python.

Homework

Java on codecademy.


Week 1-2

We learned about Discrete Math.

Homework was all written assignments found on the linked page.