Podcast
Questions and Answers
In object-oriented programming, what is the role of a class?
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?
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?
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?
Which term refers to the variables defined inside a class that represent the attributes of an object?
What is the significance of the public
keyword when declaring a class in Java?
What is the significance of the public
keyword when declaring a class in Java?
What happens if you declare more than one public
class in a single .java
file?
What happens if you declare more than one public
class in a single .java
file?
In Java, what is the purpose of the main
method?
In Java, what is the purpose of the main
method?
What is the significance of the void
keyword in a method declaration?
What is the significance of the void
keyword in a method declaration?
Which of the following describes the conventions for naming methods in Java?
Which of the following describes the conventions for naming methods in Java?
What do the empty parentheses ()
after a method's name generally indicate?
What do the empty parentheses ()
after a method's name generally indicate?
In Java, what is a 'driver class' primarily used for?
In Java, what is a 'driver class' primarily used for?
What is the purpose of the new
keyword in Java?
What is the purpose of the new
keyword in Java?
How do you call a method on an object in Java?
How do you call a method on an object in Java?
If multiple classes in a Java application contain a main
method, which main
method is executed when the application starts?
If multiple classes in a Java application contain a main
method, which main
method is executed when the application starts?
What command is used to compile Java source code files?
What command is used to compile Java source code files?
In the context of methods, what is a 'parameter'?
In the context of methods, what is a 'parameter'?
What is a 'parameter list' in a method declaration?
What is a 'parameter list' in a method declaration?
What are the values passed to a method when it is called known as?
What are the values passed to a method when it is called known as?
What happens if the number of arguments in a method call does not match the number of parameters in the method declaration?
What happens if the number of arguments in a method call does not match the number of parameters in the method declaration?
What is the significance of the java.lang
package in Java?
What is the significance of the java.lang
package in Java?
What is the 'default package' in Java?
What is the 'default package' in Java?
What is the main advantage of declaring instance variables as private
?
What is the main advantage of declaring instance variables as private
?
What is the purpose of 'getter' and 'setter' methods?
What is the purpose of 'getter' and 'setter' methods?
What happens to the values of local variables declared inside a method when the method terminates?
What happens to the values of local variables declared inside a method when the method terminates?
What is an 'instance variable'?
What is an 'instance variable'?
Which of the following is true about instance variables?
Which of the following is true about instance variables?
In Java, what happens if an instance variable is not explicitly initialized?
In Java, what happens if an instance variable is not explicitly initialized?
What are the two main categories of data types in Java?
What are the two main categories of data types in Java?
Which of the following is a primitive type in Java?
Which of the following is a primitive type in Java?
In Java, what is the default value of a boolean
instance variable if it is not explicitly initialized?
In Java, what is the default value of a boolean
instance variable if it is not explicitly initialized?
In Java, what is the default value of a String
instance variable if it is not explicitly initialized?
In Java, what is the default value of a String
instance variable if it is not explicitly initialized?
What is a 'constructor' in Java?
What is a 'constructor' in Java?
What is a key characteristic of a constructor method?
What is a key characteristic of a constructor method?
If a class does not explicitly define any constructors, what happens?
If a class does not explicitly define any constructors, what happens?
What happens if a constructor is defined with arguments?
What happens if a constructor is defined with arguments?
Which primitive type in Java is used to represent floating-point numbers with double precision?
Which primitive type in Java is used to represent floating-point numbers with double precision?
Which format specifier is used with System.out.printf
to output values of type float
or double
?
Which format specifier is used with System.out.printf
to output values of type float
or double
?
What does the JOptionPane.showMessageDialog()
method do?
What does the JOptionPane.showMessageDialog()
method do?
If the first argument in JOptionPane.showMessageDialog()
is null
, where is the dialog box displayed?
If the first argument in JOptionPane.showMessageDialog()
is null
, where is the dialog box displayed?
Which JOptionPane
method is used to prompt the user for input via a dialog box?
Which JOptionPane
method is used to prompt the user for input via a dialog box?
What does the static String format()
method do in Java?
What does the static String format()
method do in Java?
Flashcards
What is a Class?
What is a Class?
A blueprint for creating objects; defines attributes and behaviors.
What is an Object?
What is an Object?
An instance of a class; a concrete entity with its own state and behavior.
What is a Method?
What is a Method?
A function or procedure that defines the behaviors of a class.
What are Instance Variables?
What are Instance Variables?
Signup and view all the flashcards
What is a Method Call?
What is a Method Call?
Signup and view all the flashcards
What does 'public' do?
What does 'public' do?
Signup and view all the flashcards
What is the 'main' Method?
What is the 'main' Method?
Signup and view all the flashcards
What is a Return type?
What is a Return type?
Signup and view all the flashcards
What is a 'void' Return?
What is a 'void' Return?
Signup and view all the flashcards
What is a Method Header?
What is a Method Header?
Signup and view all the flashcards
What is a Parameter?
What is a Parameter?
Signup and view all the flashcards
What are Arguments?
What are Arguments?
Signup and view all the flashcards
What is a Driver Class?
What is a Driver Class?
Signup and view all the flashcards
What is 'nextLine'?
What is 'nextLine'?
Signup and view all the flashcards
What is 'next'?
What is 'next'?
Signup and view all the flashcards
What are Local Variables?
What are Local Variables?
Signup and view all the flashcards
What are Instance variables?
What are Instance variables?
Signup and view all the flashcards
What does 'private' stand for?
What does 'private' stand for?
Signup and view all the flashcards
What are 'set' and 'get'?
What are 'set' and 'get'?
Signup and view all the flashcards
What are Primitive Types?
What are Primitive Types?
Signup and view all the flashcards
What are Reference Types?
What are Reference Types?
Signup and view all the flashcards
What is a Constructor?
What is a Constructor?
Signup and view all the flashcards
What are Default Initial Values?
What are Default Initial Values?
Signup and view all the flashcards
What does Constructor do?
What does Constructor do?
Signup and view all the flashcards
What are float and double?
What are float and double?
Signup and view all the flashcards
How many digits float represents?
How many digits float represents?
Signup and view all the flashcards
What is the class for Prebuilt Dialog Boxes?
What is the class for Prebuilt Dialog Boxes?
Signup and view all the flashcards
What does GUIs stand for?
What does GUIs stand for?
Signup and view all the flashcards
What does showMessageDialog() do?
What does showMessageDialog() do?
Signup and view all the flashcards
What does showInputDialog() do?
What does showInputDialog() do?
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 bynextLine
. 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
andString
are contained withinjava.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
andgetCourseName
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 benull
.
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
forbyte
,char
,short
,int
,long
,float
anddouble
, otherwisenull
. - 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 asmessage 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.