Object-Oriented Programming: Classes and Objects

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In object-oriented programming, what is the role of a class?

  • To perform a specific task within a program.
  • To serve as a blueprint for creating objects. (correct)
  • To manage the flow of execution in a program.
  • To store data temporarily during program execution.

Which of the following best describes an 'object' in the context of object-oriented programming?

  • A collection of related classes.
  • An instance of a class. (correct)
  • A programming language keyword.
  • A set of instructions that the computer follows.

What is the primary function of a 'method' within a class?

  • To define the characteristics or attributes of an object.
  • To create new instances of the class.
  • To store the state of an object.
  • To perform operations or actions on an object's data. (correct)

Which term refers to the variables defined inside a class that represent the attributes of an object?

<p>Instance variables (A)</p>
Signup and view all the answers

What is the significance of the public keyword when declaring a class in Java?

<p>It indicates that the class is available to all parts of the program and other programs. (C)</p>
Signup and view all the answers

What happens if you declare more than one public class in a single .java file?

<p>A compilation error will occur. (D)</p>
Signup and view all the answers

In Java, what is the purpose of the main method?

<p>It is the entry point of a Java program. (D)</p>
Signup and view all the answers

What is the significance of the void keyword in a method declaration?

<p>It indicates that the method does not return any value. (C)</p>
Signup and view all the answers

Which of the following describes the conventions for naming methods in Java?

<p>Method names should start with a lowercase letter, and subsequent words should start with a capital letter. (D)</p>
Signup and view all the answers

What do the empty parentheses () after a method's name generally indicate?

<p>The method does not require any additional information to perform its task. (A)</p>
Signup and view all the answers

In Java, what is a 'driver class' primarily used for?

<p>Testing and executing other classes. (D)</p>
Signup and view all the answers

What is the purpose of the new keyword in Java?

<p>To create a new object of a specified class. (C)</p>
Signup and view all the answers

How do you call a method on an object in Java?

<p>Using the object name followed by a dot (<code>.</code>) and the method name. (A)</p>
Signup and view all the answers

If multiple classes in a Java application contain a main method, which main method is executed when the application starts?

<p>The <code>main</code> method in the class specified when running the <code>java</code> command. (B)</p>
Signup and view all the answers

What command is used to compile Java source code files?

<p><code>javac</code> (B)</p>
Signup and view all the answers

In the context of methods, what is a 'parameter'?

<p>Additional information a method requires to perform its task. (B)</p>
Signup and view all the answers

What is a 'parameter list' in a method declaration?

<p>A comma-separated list of parameters that the method accepts. (B)</p>
Signup and view all the answers

What are the values passed to a method when it is called known as?

<p>Arguments (A)</p>
Signup and view all the answers

What happens if the number of arguments in a method call does not match the number of parameters in the method declaration?

<p>A compilation error will occur. (D)</p>
Signup and view all the answers

What is the significance of the java.lang package in Java?

<p>It contains classes that are implicitly imported into every Java program. (B)</p>
Signup and view all the answers

What is the 'default package' in Java?

<p>The package for classes compiled in the same directory on disk. (A)</p>
Signup and view all the answers

What is the main advantage of declaring instance variables as private?

<p>It prevents instance variables from being accidentally modified by other classes. (C)</p>
Signup and view all the answers

What is the purpose of 'getter' and 'setter' methods?

<p>To provide controlled access to private instance variables. (A)</p>
Signup and view all the answers

What happens to the values of local variables declared inside a method when the method terminates?

<p>They are lost. (D)</p>
Signup and view all the answers

What is an 'instance variable'?

<p>A variable that is unique to each object of a class. (A)</p>
Signup and view all the answers

Which of the following is true about instance variables?

<p>Each object of a class has its own copy of the instance variables. (B)</p>
Signup and view all the answers

In Java, what happens if an instance variable is not explicitly initialized?

<p>The variable is assigned a default value based on its type. (A)</p>
Signup and view all the answers

What are the two main categories of data types in Java?

<p>Primitive types and reference types (D)</p>
Signup and view all the answers

Which of the following is a primitive type in Java?

<p><code>int</code> (B)</p>
Signup and view all the answers

In Java, what is the default value of a boolean instance variable if it is not explicitly initialized?

<p><code>false</code> (A)</p>
Signup and view all the answers

In Java, what is the default value of a String instance variable if it is not explicitly initialized?

<p><code>null</code> (B)</p>
Signup and view all the answers

What is a 'constructor' in Java?

<p>A special method used to initialize objects. (A)</p>
Signup and view all the answers

What is a key characteristic of a constructor method?

<p>It has the same name as the class. (D)</p>
Signup and view all the answers

If a class does not explicitly define any constructors, what happens?

<p>Java provides a default constructor with no parameters. (A)</p>
Signup and view all the answers

What happens if a constructor is defined with arguments?

<p>The Java compiler will not create a default no-argument constructor. (A)</p>
Signup and view all the answers

Which primitive type in Java is used to represent floating-point numbers with double precision?

<p>double (A)</p>
Signup and view all the answers

Which format specifier is used with System.out.printf to output values of type float or double?

<p><code>%f</code> (C)</p>
Signup and view all the answers

What does the JOptionPane.showMessageDialog() method do?

<p>Displays a dialog box containing a message. (B)</p>
Signup and view all the answers

If the first argument in JOptionPane.showMessageDialog() is null, where is the dialog box displayed?

<p>Center of the screen (A)</p>
Signup and view all the answers

Which JOptionPane method is used to prompt the user for input via a dialog box?

<p><code>showInputDialog</code> (D)</p>
Signup and view all the answers

What does the static String format() method do in Java?

<p>It applies formatting to a string. (B)</p>
Signup and view all the answers

Flashcards

What is a Class?

A blueprint for creating objects; defines attributes and behaviors.

What is an Object?

An instance of a class; a concrete entity with its own state and behavior.

What is a Method?

A function or procedure that defines the behaviors of a class.

What are Instance Variables?

Variables that hold the attributes and characteristics of a class.

Signup and view all the flashcards

What is a Method Call?

A request sent to an object to execute a specific method.

Signup and view all the flashcards

What does 'public' do?

Ensures that a class is 'available to the public'

Signup and view all the flashcards

What is the 'main' Method?

Automatically called by JVM when an application is executed.

Signup and view all the flashcards

What is a Return type?

Specifies data type returned after a method performs its task.

Signup and view all the flashcards

What is a 'void' Return?

A method that performs a task but does not return any information.

Signup and view all the flashcards

What is a Method Header?

The first line of the method, contains all the information.

Signup and view all the flashcards

What is a Parameter?

Extra info to perform a method's task.

Signup and view all the flashcards

What are Arguments?

Represents values supplied for method's parameters.

Signup and view all the flashcards

What is a Driver Class?

A class used to test another class

Signup and view all the flashcards

What is 'nextLine'?

Reads characters until a newline character is encountered.

Signup and view all the flashcards

What is 'next'?

Reads a single word

Signup and view all the flashcards

What are Local Variables?

Variables declared within a method's body.

Signup and view all the flashcards

What are Instance variables?

Variables that retains when a method terminates.

Signup and view all the flashcards

What does 'private' stand for?

An access modifier to make variables only accessible only to the class methods.

Signup and view all the flashcards

What are 'set' and 'get'?

Methods to access private and set variables.

Signup and view all the flashcards

What are Primitive Types?

boolean, byte, char, short, int, long, float, and double.

Signup and view all the flashcards

What are Reference Types?

Stores the memory locations; also knows as sending messages to objects

Signup and view all the flashcards

What is a Constructor?

Initializes a new created object of a class.

Signup and view all the flashcards

What are Default Initial Values?

Initializes a field if you do not provide a field's initialization

Signup and view all the flashcards

What does Constructor do?

Each class can provide.

Signup and view all the flashcards

What are float and double?

Floating-point primitive types.

Signup and view all the flashcards

How many digits float represents?

7 significant digits.

Signup and view all the flashcards

What is the class for Prebuilt Dialog Boxes?

JOptionPane Class.

Signup and view all the flashcards

What does GUIs stand for?

Graphical user interfaces.

Signup and view all the flashcards

What does showMessageDialog() do?

Displays a dialog box containing a message.

Signup and view all the flashcards

What does showInputDialog() do?

Allows user to enter data into a program, display a field known as text field.

Signup and view all the flashcards

Study Notes

  • The lecture introduces classes and objects in object-oriented programming.
  • The course code BMED-2111 focuses on Object-Oriented Programming.

Objectives

  • Understand classes, objects, methods, and instance variables.
  • Learn to declare a class and create objects from it.
  • Implement class behaviors by declaring methods within a class.
  • Implement class attributes by declaring instance variables.
  • Call an object's methods to execute tasks.
  • Differentiate between instance variables and local method variables
  • Use constructors to initialize object data upon creation.
  • Understand the differences between primitive and reference types.

Introduction

  • Classes are a core concept.
  • Objects are instances of classes
  • Methods define actions or behaviors.
  • Parameters are inputs to methods.
  • The double primitive type is covered.

Classes, Objects, Methods, and Instance Variables

  • Analogy: Relate classes to car design and operation.
  • Imagine pressing an accelerator pedal to make a car go faster.
  • Cars need to be designed before they can be driven.
  • Engineering drawings are to cars like blueprints are to houses.
  • Car designs include an accelerator pedal design.
  • Pedals hide complex mechanisms, just as a brake pedal hides the slowing mechanism.
  • This allows people to drive cars easily without engine knowledge.
  • Cars must be built from engineering drawings.
  • Drivers press an accelerator pedal to send commands to the car.
  • Methods are required to perform tasks in a program.
  • Methods describe how to perform the task.
  • A method hides complex tasks from the user.
  • In Java, a class contains methods, as a car's engineering drawings include the accelerator pedal design.
  • A class provides methods to perform tasks.
  • Programs perform tasks by using objects of a class.
  • Java is object-oriented.
  • Pressing a gas pedal in a car sends a message to make the car go faster.
  • Sending messages to an object is a method call.
  • The method call directs the object to perform its task.
  • A car has attributes like color, number of doors, gas in the tank, speed, and miles driven.
  • A car's attributes are in its engineering diagrams.
  • Each car maintains its own attributes.
  • Cars know how much gas they have, without knowing how much other cars have.
  • Objects have attributes carried with them.
  • Attributes are specified as part of the object's class.
  • Bank accounts have balances which are the amount of money in the account.
  • Each knows its balance, but not the balances of other accounts.
  • Instance variables specify attributes.

Declaring a Class with a Method and Instantiating an Object of a Class

  • You can create a new class such as GradeBook.
  • You can use a class to create an object.
  • Public classes must be stored in files named after the class with a .java extension.
  • The "public" keyword is an access modifier.
  • The public keyword signifies that the class is available to the public.
  • Declaring more than one public class within a single file results in a compilation error.
  • main is called automatically by the Java Virtual Machine (JVM).
  • Usually methods must be called to perform any tasks.
  • A public method is available to the public.
  • Return type is the data type the method returns after being run.
  • Return type void signifies that the method will perform a task but not return information to its caller after it has completed.
  • A method name goes after the return type.
  • Method names by convention begin with a lowercase letter with subsequent words are capitalized.
  • Empty parentheses indicate it does not require additional information.
  • The method header has the first line of the method code.
  • The braces delimit the method’s body.
  • The method body tells what task is performed by the program
  • To use a class you must declare one within the program.
  • The GradeBook class is shown as an example.
  • main is essential if a class is to be run.
  • Error message will be thrown without a main method: NoSuchMethodError:main .
  • A static method is special.
  • Static methods are called without creating objects in the class in which the class is declared.
  • You can’t call a method unless you create an object.
  • Each class becomes a new type to declare variables and create objects.
  • Classes are extensible, as new class types can be declared.
  • new keyword creates a new object with the class specified to the right of the keyboard.
  • The new keyword is used to initialize a variable.
  • Putting parentheses at the right of a class name is calling a constructor.
  • The class-type variable dictates how you will call the method.
  • Variables are named, followed by a period or "dot separator" then the named
  • Calls make the method preform its task.
  • Main is contained in all classes, the JVM will call in the class used to execute the application.
  • A compile command for the class is javac Gradebook.java GradeBookTest.java.
  • You can compile within a directory by running javac *.java.

Declaring a Method with a Parameter

  • The gas pedal makes a car go faster.
  • Pressing the pedal further accelerates the car more.
  • A message can include the task to preform and other information that helps the car perform the task, just as a parameter provides additional information for a method to preform it’s task.
  • Methods can require one or more parameters, separated by commas and listed in the parenthesis that follow the method name.
  • Each parameter must state a type and identifier.
  • A method call supplies values (arguments) for each of the methods parameters.
  • nextLine reads the inputted characters until they are ended, and returns a String.
  • Enter submits the string, and inserts a new line at the end.
  • The newline is discarded by nextLine.
  • next reads individual words.
  • next reads inputted characters until it encounters a whitespace character, and returns a String, discarding the whitespace.
  • If there is information after the first whitespace, other statements can be use scan it.
  • When calling arguments, match the parameters.
  • Argument type must be the name as the parameters of the call, ensuring consistency.
  • System and String are contained within java.lang.
  • java.lang is imported into every Java program implicitly.
  • User-defined classes must be imported with the import command.
  • If classes are compiled to the same directory , they are in the same package called the default package, and exist under one namespace.
  • If you use fully qualified class names, you don't have to import.
  • import declarations are more convenient as most Java programmers prefer use of those rather than long namespace addressing.

Instance Variables, set Methods and get Methods

  • Local variables: Variables declared for each method and cannot be used by other methods after they loose them.
  • Java carries objects, and attributes that existed after they execute
  • Classes typically consist of manipulated by a class methods.
  • Attributes and variables in class declarations.
  • Called fields, declared that class but not its methods.
  • When an object of a class exists its called an instance variable
  • Each has a variable within their memory.
  • Instances declared typically exist as private.
  • Private is an access modifier that can only be accessed by methods within the class.
  • Data hiding is when an instance is labeled as private.
  • Variables that are private and can be only accessed by the methods within the object.
  • This prevents classes in other programs being modified.
  • A "set" method is typically used as a modifier to the values of the variable, while a "get" method retrieves the values of a variable.
  • Instance variables should be listed with access modifiers.
  • "Get" and "set" are just suggested conventions, the prefix is not essential.
  • Return types should be labeled for methods with a void return type.
  • setCourseName and getCourseName help the class in each of the class's methods.
  • Static methods do no follow the rule, more information can be found in Chapter 8.
  • Methods not defined does not matter with respect to order of execution but only when they are called.
  • Methods can call each other within a class just with their first name.
  • Variables are initialized using the default field value of Java.
  • Variables that are defined within class is required to have values, local however do not need explicit values.
  • The default value for a field is for a String to be null.

Primitive Types vs. Reference Types

  • Every field has default values even null strings as defined in each Java implementation.
  • There are primitive types and reference types.
  • Primitive types include boolean, byte, char, short, int, long, float, and double.
  • Non-primitive types are reference types.
  • A primitive-type variable can store exactly one value of its declared type at a time.
  • Primitive-type instance variables are initialized by default to 0 for byte, char, short, int, long, float and double, otherwise null.
  • Booleans will default to false.
  • Specificity is given by the variable within the declaration.
  • Attempting to use a uninitialized variable will return a compiler error.
  • References can be referenced and contain many methods.
  • Objects are contained as a null value, or "nothing", reserved for the memory.
  • The object needs to be invoked to work.

Initializing Objects with Constructors

  • When an object is created its instance variables are set to default values.
  • Every class provides a constructor to the specific object during declaration.
  • Java calls new to assign a constructor in memory.
  • Constructors will have the same name as the class
  • By default compilers assigns with no parameters in any, but the values have to be customized.
  • Parameters list out the information that the constructor needs to take and perform.
  • You typically can't return types of data.
  • They are typically labelled as public.
  • You should initialize your variables otherwise Java can cause problems by assigning default values.
  • These functions are specified inline.

Floating-Point Numbers and Type double

  • Floating numbers are variables such as 2.3, 0.242.
  • Float and doubles are primitive types.
  • Doubles can hold larger numbers and finer detail.
  • float represents single-precision to 7 digits.
  • 15 digits is roughly twice the numbers for double.
  • Double values are what Java uses by default for floating values.
  • Refer to the appendix to check ranges for variables.
  • Floating point numbers are hard to represent precisely which may result in improper usage.
  • Use System.out.printf("%.2f\n"); in which %.2f denotes two digits to the name,
  • If you do not enter the decimal it rounds down to the nearest placement.
  • System scanner double is scanner.nextDouble.

GUI and Graphics

  • Can display text.
  • The function JOptionPane can display windows, such as message dialogs.
  • javax.swing can create GUIs.
  • GUIs makes the data entry and display available to the user.
  • JOptionPane.showMessageDialog shows the users a message
    • Requires 2 arguments
    • The first positions dialogue based on location in which null makes its center
    • The second states the message and string to display.
  • It uses static methods.
  • They are always available, called name with ClassName.methodName.
  • It allows uses to enter the data.
  • Prompts user and field in text.
  • When the function is entered it returns a strings of characters.
  • Canceled values can be null.
  • String format works like System.out.printf except output is a string.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

C++ Classes & Objects: Introduction to OOP
12 questions
Object-Oriented Programming Fundamentals
10 questions
OOP with Java: Classes and Objects
8 questions
Use Quizgecko on...
Browser
Browser