AP Computer Science A -- Using Objects


Introduction to Objects | Methods | String Objects | Wrapper Classes | Math Class | Review | Resources


Introduction to Objects

The information we manage in a Java program is either represented as primitive data or as objects. Objects are the building blocks of object-oriented programing. Software objects are conceptually similar to real-world objects in that objects consist of states and related behavior. An object stores its state in fields (such as variables) and exposes its behavior through methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation - a fundamental principle of object-oriented programming.

Assignment: Introduction to Objects Video Check

Directions: After watching and studying the video on Introduction to Objects, complete the Introduction to Objects video check in itsLearning. You will only have a few minutes to complete the check as it is expected that you have reviewed the material before beginning the check.

Assignment: Question for Thought 2.1

Directions: Answer the following questions:

  1. How do you compute total square feet?
  2. How do you compute circumference?
  3. Research java.util.Scanner; What does it do and why do you need to use it?
  4. Define each primitive data type and give an example:

Lab: Painting a Room

Directions: Download the Painting a Room Lab. Follow the instructions on the lab sheet to create a program that will calculate the number of gallons of paint needed to paint a room. Submit your code as well as a screenshot from the BlueJ terminal window. Do not remove the comments from the source code. They indicate where to put the code.

Lab: Area and Circumference of a Circle

Directions: Download the Area and Circumference of a Circle Lab. Follow the instructions on the lab sheet to create a program that will calculate the area and circumference of a circle. Submit your code as well as a screenshot from the BlueJ terminal window. Do not remove any the comments from the source code. They indicate where to put the code.


Reference Data Types

The information that we manage in a Java program is either represented as primitive data or as objects. In Java, everything that is not a primitive data type is an object. The first type of object that we have seen so far is a String. Strings are made up of a series of characters.

Java key conceptA reference variable does not contain its object's actual data in its memory location, since object sizes can vary. Instead, the variable contains the reference (also referred to as "memory address") of the object it refers to. After an object reference variable has been declared, it must be initialized before it can be used. For an object, initializing means we must make sure the variable refers to a valid object before trying to use it

We create an object with the new operator, using a process known as instantiation. An object is an instance of a particular class. After the new operator creates the object, a constructor constructs (builds) the object. A constructor has the same name as the class and is like a method.

The class is a blue print or set of instructions for making an object. A constructor reads the class (instructions) and builds the objects.

After an object has been instantiated, we use the dot operator to get its methods. The dot operator (single period character) is added right after the object reference and is followed by the method being invoked. In the example below, we use the dot operator to separate the println method from the System.out object reference.

Example of using the dot operator

Practice: String Literals and Concatenation

Directions: Download the String Literals and Concatenation practice handout and write the output for the println methods. Hand the worksheet in or take a clear picture and upload to itsLearning textbox. Please note that if Mrs. Rush cannot read the picture, you will be asked to resubmit or receive a grade of zero.

Lab: Two Meanings of Plus

Directions: In Java, the symbol + can be used to add numbers or to concatenate (join together) strings. This lab illustrates both uses. When using a string literal (a sequence of characters enclosed in double quotation marks) in Java the complete string must fit on one line. The following is NOT legal (it would result in a compile-time error). The solution is to break the long string up into two shorter strings that are joined using the concatenation operator (which is the + symbol). So, when working with strings the + symbol means to concatenate the strings (join them). BUT, when working with numbers the + means what it has always meant-add!

Download the Two Meanings of Plus lab and complete. Write the answers to questions 1, 2 & 3 under section C in the instructions (paste directly into the itsLearning textbox) and paste in your code from #2 as well as embed a screenshot from the BlueJ terminal window to the itsLearning textbox.


String Objects & Methods

high levelAn object reference variable stores the address where an object is stored in memory.

Assignment: String Objects Video Check

Directions: After watching and studying the video on String Objects, complete the String Objects video check in itsLearning. You will only have a few minutes to complete the check as it is expected that you have reviewed the material before beginning the check.

String Methods

As a review, there are a number of useful methods when working with Strings (use the arrows at the bottom of the slide to advance to the next slide).

Slide Set created by Shannon Anderson-Rush with GoConqr

compareTo card

Assignment: String Methods Video Check

Directions: After watching and studying the video on String Methods, complete the String Methods video check in itsLearning. You will only have a few minutes to complete the check as it is expected that you have reviewed the material before beginning the check.

Assignment: Intro to Strings Worksheet

Directions: Complete the worksheet in itsLearning. Given the following variable declarations, evaluate each of the given expressions. Any whitespace shown in a String is a single space character, unless otherwise indicated. Remember that char values have single quotes around them: 'c' is a single primitive character, while Strings with double quotes around them: "C" is a String that happens to have only one character in it. There is a difference!

int a = 5, b = 12, i0 = 0, il = 1, i2 = 2, i3 = 3;

char c = 'u', d = ',';

String s1 = "Hello, world!", s2 = "I love Computer Science.";

Lab: Working with Strings

Directions: Download the Working with Strings document and StringManips.java file. Follow the instructions to carefully study the Java application in BlueJ and then modify the file. When you are finished, copy and paste your code into the itsLearning textbox and embed a screenshot from BlueJ. Make sure you have updated the comment at the top of the code.

Assignment: Line Editor FRQ

Directions: Download the line editor FRQ from itsLearning. Read the directions and study the examples carefully. You must handwrite both parts of the FRQ and follow the instructions for submitting in order to receive credit. For part (a), you will write the method insert for the LineEditor class. For part (b), you will write the method delete for the LineEditor class.


Classes, Libraries & Packages

The Scanner class allows us to create a scanner to get input from the user. This will make it possible for the user to type information using the keyboard in order to interact with our programs.

To use the Scanner class, you will need to import it from the Java.util library.

To create a Scanner object, you will need this line of code:

Scanner scan = new Scanner (System.in);

The Scanner class has a number of methods that allow it to be useful. For example, if you wanted to accept anticipated input from the user as an integer, you might use this line (scan only needs to be declared and instantiated one time in your program):

System.out.println("Enter an integer: ");
variableName = scan.nextInt();

The lines of code above, allow the user to enter a value (of whatever data type variableName is declared as and that value to be stored in the variable variableName. The .nextInt method allows us to receive and store an integer.

Wrapper Class

Each of the wrapper classes also has a toString() method that converts a value from a primitive type into a String object. Calling Integer.toString(999) will, for example, return the string "999".

Each of the numeric wrapper classes also has two useful constants that let us know the minimum and maximum values for that type. The constants have the names MAX_VALUE and MIN_VALUE. So Byte.MIN_VALUE would tell us the smallest type value, and Float.MAX_VALUE would tell us the largest float value.

Assignment: Question for Thought 2.1

Directions: Answer the following questions:

  1. How do you compute total square feet?
  2. How do you compute circumference?
  3. Research java.util.Scanner; What does it do and why do you need to use it?
  4. Define each primitive data type and give an example:


Math Class

Math.random( ) returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Each run should produce a different sequence of pseudo-random numbers.

Discussion: Random Method

Directions: Look through the AP Computer Science Quick Reference Guide for all of the places that the random method is used. Post your findings under the Discussions.

Assignment: Generating Random Numbers Worksheet

Directions: Download the Math.random instruction sheet and the Random Numbers Worksheet. Use the Math Random Instruction Sheet to complete the Math Random Methods Worksheet in itsLearning. For exercises 1 to 5, indicate the range of the possible result of each expression. For exercises 6 to 13, write an expression using the Math.random ( ) generates.

Complete the worksheet and submit your answers in itsLearning.

dice

Lab: Rolling Dice

Directions: Download the Rolling Dice Lab. Write a complete Java program that simulates the rolling of a pair of dice. For each die in the pair, the program should generate a random number between 1 and 6 (inclusive). It should print out the result of the roll for each die and the total roll (the sum of the two dice), all appropriately labeled. You must use the random method from the Math class. Submit your code as well as a screenshot from the BlueJ terminal window. Don't forget to update the comment at the top of the code with your name and date.


Directions: To enter the break out room, click on the picture above. Good luck!


Assignment: Unit 2 Review Worksheet

Directions: Complete the Unit 2 review worksheet in itsLearning to prepare for the unit assessment.


Review

To prepare for the unit assessment, you should utilize the stude guide.

Flash Card Deck created by sarush with GoConqr

escape room

Resources

Introduction to Objects presentation

Introduction to Primitive Data types presentation

Variables presentation

Arithmetic Expressions & Data Conversion presentation

String Objects & Methods presentation

Classes, Libraries, & Packages presentation

The Math Class presentation

Sources

Primitive Data Types - Summary image: https://corejava25hours.com/category/java/

Autoboxing image: https://corejava25hours.com/category/java/

CompareTo method card: https://cms.gavirtualschool.org/Shared/Electives/AP_ComputerScience_13/APCompSci_ObjOrienProg/APCompSci_ObjOrienProg_Shared2.html

Dice: public domain

Type Casting image: https://javarevisited.blogspot.com/2012/12/what-is-type-casting-in-java-class-interface-example.html

Transcript