Unit 2.1-2.3

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/46

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

47 Terms

1
New cards

Java is an object-oriented programming language. That means that one of the primary ways of organizing our programs is in terms of objects. Objects are a kind of value that combines data and the code that operates on that data into a single unit. Objects are defined in Java by writing classes which provide a blueprint for creating objects of a certain kind, describing the data and code that all instances of that class have.

2
New cards

But in Java all programs are built out of classes. This is why, as you saw in Unit 1, every Java programs starts with public class: the first thing we have to do when we write a Java program is define at least one class.

3
New cards

As a blueprint, each class defines the attributes its objects have (the properties or what each object knows about itself) and the behaviors (what each object can do). In Java code, the attributes are written as instance variables in the class, and the behaviors are written as methods.

4
New cards

object=an instance of a class

5
New cards

The dot operator (.) is used to run an object’s method. You can think of the (.) as asking the object to do something (execute one of its methods). For example, yertle.forward() asks the turtle yertle to go forward. It doesn’t tell yertle how much to go forward, so it goes forward 100 pixels by default. The parentheses () after a method name are there in case you need to give the method arguments (some data) to do its job,

6
New cards

// To create or declare a new object, write:// ClassName variableName = new ClassName(arguments);Turtle yourTurtleName =new Turtle(habitat); yourTurtlename.forward();

7
New cards

class defines a new data type (a classification). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.

8
New cards

An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.

9
New cards

An attribute or instance variable is data the object knows about itself. For example a turtle object knows the direction it is facing or its color.

10
New cards

behavior or method is something that an object can do. For example a turtle object can go forward 100 pixels.

11
New cards

A Java class defines what objects of the class know (attributes) and what they can do (behaviors). Each class has constructors like World() and Turtle(habitat) which are used to initialize the attributes in a newly created object.

12
New cards

A new object is created with the new keyword followed by the class name (new Class()). When this code executes, it creates a new object of the specified class and calls a constructor, which has the same name as the class. For example, new World() creates and initializes a new object of the World class, and new Turtle(habitat) creates and initializes a new Turtle object in the World habitat.

13
New cards

// To create a new object and call a constructor write:// ClassName variableName = new ClassName(parameters);World habitat = new World(); // create a new World objectTurtle t = new Turtle(habitat); // create a new Turtle object

14
New cards

There can be more than one constructor defined in a class. This is called overloading the constructor. There is usually a constructor that has no parameters (nothing inside the parentheses following the name of the constructor) like the World() constructor above. This is also called the no-argument constructor. The no-argument constructor usually sets the attributes of the object to default values.

15
New cards

There can also be other constructors that take parameters like the Turtle(habitat) constructor call above. A parameter (also called actual parameter or argument) is a value that is passed into a constructor. It can be used to initialize the attribute of an object.

16
New cards

There can also be other constructors that take parameters like the Turtle(habitat) constructor call above. A parameter (also called actual parameter or argument) is a value that is passed into a constructor. It can be used to initialize the attribute of an object.

17
New cards

You can also declare an object variable and initialize it to null (Turtle t1 = null;). An object variable holds a reference to an object. A reference is a way to find the object in memory. It is like a tracking number that you can use to track the location of a package.

18
New cards

The code Turtle t1 = null; creates a variable t1 that refers to a Turtle object, but the null means that it doesn’t refer to an object yet. You could later create the object and set the object variable to refer to that new object (t1 = new Turtle(world1)). Or more commonly, you can declare an object variable and initialize it in the same line of code (Turtle t2 = new Turtle(world1);).

World world1 = **new** World(); Turtle t1 = **null**; t1 = **new** Turtle(world1); *// declare and initialize t2*Turtle t2 = **new** Turtle(world1);

19
New cards

When you use a class that someone has already written for you in a library that you can import like the Turtle class above, you can look up how to use the constructors and methods in the documentation for that class. The documentation will list the signatures  like public (or headers) of the constructors or methods which will tell you their name and parameter list. The parameter list, in the header of a constructor, lists the formal parameters, declaring the variables that will be passed in as values and their data types.

20
New cards

Constructors are overloaded when there are multiple constructors, but the constructors have different signatures. They can differ in the number, type, and/or order of parameters. For example, here are two constructors for the Turtle class that take different parameters:

21
New cards

the formal parameters, (year, month, day), are set to copies of the actual parameters (or arguments), which are (2005,9,1).

22
New cards

This is call by value which means that copies of the actual parameter values are passed to the constructor. These values are used to initialize the object’s attributes.

23
New cards

Constructors initialize the attributes in newly created objects. They have the same name as the class.

24
New cards

constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.

25
New cards

Overloading is when there is more than one constructor. They must differ in the number, type, or order of parameters.

26
New cards

New is a keyword that is used to create a new object of a class. The syntax is new ClassName(). It creates a new object of the specified class and calls a constructor.

27
New cards

no-argument constructor is a constructor that doesn’t take any passed in values (arguments).

28
New cards

Parameters allow values to be passed to the constructor to initialize the newly created object’s attributes.

29
New cards

The parameter list, in the header of a constructor, is a list of the type of the value being passed and a variable name. These variables are called the formal parameters.

30
New cards

Actual parameters are the values being passed to a constructor. The formal parameters are set to a copy of the value of the actual parameters.

31
New cards

Formal parameters are the specification of the parameters in the constructor header. In Java this is a list of the type and name for each parameter (World(int width, int height).

32
New cards

Call by value means that when you pass a value to a constructor or method it passes a copy of the value.

33
New cards

Methods are a set of instructions that define behaviors for all objects of a class. 

34
New cards

To use an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example, yertle.forward(); calls yertle’s forward method to move a turtle object forward 100 pixels. These are called object methods or non-static methods. An object method must be called on an object of the class that the method is defined in. Object methods work with the attributes of the object, such as the direction the turtle is heading or its position.

35
New cards

Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works. For example, we know that if we hit the brakes, the car will stop, and we can still use the brakes even if we don’t really know how they work.

36
New cards

When we use methods for a class in a library, we can look up the method signature (or method header), which is the method name followed by a parameter list, in its documentation. 

37
New cards

Methods inside the same class can call each other using just methodName(), but to call non-static methods in another class or from a main method, you must first create an object of that class and then call its methods using object.methodName().

38
New cards

method(); is used to call a method within the same class, but object.method(); is necessary if you are calling the method from the main method or from a different class.

39
New cards

Remember that if you just declare an object reference without setting it to refer to a new object the value will be null meaning that it doesn’t reference an object. If you call a method on a variable whose value is null, you will get a NullPointerException error, where a pointer is another name for a reference.

40
New cards

Methods are a set of instructions that define the behaviors for all objects of the class.

41
New cards

Use dot notation to execute an object’s method. This is the object’s name followed by the dot (.) operator followed by the method name and parentheses: object.method();

42
New cards

method signature is the method name followed by the parameter list which gives the type and name for each parameter. Note that methods do not have to take any parameters, but you still need the parentheses after the method name.

43
New cards

Procedural abstraction allows a programmer to use a method by knowing in general what it does without knowing what lines of code execute. This is how we can drive a car without knowing how the brakes work.

44
New cards

method or constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing. Once the last statement in the method or constructor has executed or a return statement is executed, the flow of control is returned to the point immediately following the method or constructor call.

45
New cards

NullPointerException will happen if you try to call an object method on an object variable whose value is null. This usually means that you forgot to create the object using the new operator followed by the class name and parentheses.

46
New cards

An object method or non-static method is one that must be called on an object of a class. It usually works with the object’s attributes.

47
New cards

static method or class method method is one that doesn’t need to be called on an object of a class.