SWENG3101 Object Oriented Programming Chapter 3
45 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the default radius value when a CircleConstructor object is created without parameters?

  • 0.0
  • 20.0
  • 10.0
  • 1.0 (correct)
  • The area method in the CircleConstructor class returns the area using the formula $ ext{area} = ext{radius} imes 3.14$.

    False

    What are accessor and mutator methods used for in a class?

    To allow access to private instance variables and to modify their values.

    A method that sets a value for a private variable is called a __________ method.

    <p>mutator</p> Signup and view all the answers

    Match the following objects with their corresponding area values:

    <p>circleA = 1256.0 circleB = 314.0 circleC = 3.14 circleD = Unknown</p> Signup and view all the answers

    What default value is assigned to object references in Java?

    <p>null</p> Signup and view all the answers

    The salary variable in the Employee class is accessible in child classes.

    <p>False</p> Signup and view all the answers

    In the Bank class, what method is used to update the account balance?

    <p>deposite</p> Signup and view all the answers

    The _____ keyword is used to declare class variables in Java.

    <p>static</p> Signup and view all the answers

    Match the following methods with their functionality in the Bank class:

    <p>setbalance = Sets the initial balance deposite = Increases the balance by a specified amount withdraw = Decreases the balance by a specified amount balance = Stores the current balance</p> Signup and view all the answers

    What will be the output of the following code: 'System.out.println(foo.x);' in the Foo class?

    <p>false</p> Signup and view all the answers

    Values in Java can only be assigned during the declaration.

    <p>False</p> Signup and view all the answers

    Which method in the Employee class prints the employee details?

    <p>printEmp</p> Signup and view all the answers

    What is encapsulation in programming?

    <p>The process of combining data and implementation details into a single unit</p> Signup and view all the answers

    A private member of a class can be accessed directly from a different class that is in the same package.

    <p>False</p> Signup and view all the answers

    What is the nature of static variables in a class?

    <p>Only one copy exists per class.</p> Signup and view all the answers

    List the four access modifiers provided by Java.

    <p>private, protected, public, default</p> Signup and view all the answers

    In Java, if no access modifier is specified, the member is accessible within the _______ only.

    <p>package</p> Signup and view all the answers

    Static variables are created when the program stops.

    <p>False</p> Signup and view all the answers

    Which of the following statements about public access in Java is true?

    <p>Public members can be accessed anywhere the class is visible.</p> Signup and view all the answers

    What is the purpose of a method in Java?

    <p>To describe the behavior of an object.</p> Signup and view all the answers

    Protected members are accessible in subclasses regardless of the package.

    <p>True</p> Signup and view all the answers

    In the class Employee2, the variable DEPARTMENT is declared as a ________.

    <p>constant</p> Signup and view all the answers

    Match the following Java concepts with their descriptions:

    <p>Instance variable = Unique to each object Static variable = Common to all instances Method = Behavior descriptor of an object Constructor = Initializes new objects</p> Signup and view all the answers

    What is the purpose of accessor methods in a class?

    <p>To provide controlled access to private members.</p> Signup and view all the answers

    Match the following access modifiers with their accessibility:

    <p>public = Accessible anywhere protected = Accessible in package and subclasses private = Accessible only within the class default = Accessible within the package only</p> Signup and view all the answers

    In the Counter class example, what will be the output of the program when c1, c2, and c3 call printCounter()?

    <p>1, 2, 3</p> Signup and view all the answers

    Static variables are commonly used for instance-specific data.

    <p>False</p> Signup and view all the answers

    In the Employee3 class, what is the purpose of the setSalary method?

    <p>To assign a value to the initial salary variable.</p> Signup and view all the answers

    What is the primary purpose of using packages in Java?

    <p>To avoid name conflicts and organize classes</p> Signup and view all the answers

    User-defined packages are automatically imported in Java.

    <p>False</p> Signup and view all the answers

    What command is used to create a package in Java?

    <p>package</p> Signup and view all the answers

    The package that contains classes fundamental to Java programming is called __________.

    <p>java.lang</p> Signup and view all the answers

    Match the following Java packages with their appropriate descriptions:

    <p>java.lang = Contains classes fundamental to Java programming java.util = Contains utility classes for data structures java.io = Contains classes for supporting input/output operations java.awt = Contains classes for graphical user interface components</p> Signup and view all the answers

    When importing a package, what happens to its subpackages?

    <p>No subpackages are imported automatically</p> Signup and view all the answers

    Creating packages is optional for Java program development.

    <p>True</p> Signup and view all the answers

    Give an example of a built-in Java package.

    <p>java.util</p> Signup and view all the answers

    What is the purpose of the package RelationEg?

    <p>To manage multiple related files</p> Signup and view all the answers

    The getMax method returns the smaller of two integers.

    <p>False</p> Signup and view all the answers

    What does static import allow you to do in Java?

    <p>Access static members directly without using the class name.</p> Signup and view all the answers

    In Java, a class to calculate the age of a person is called ______.

    <p>CalAge</p> Signup and view all the answers

    Match the following tasks with their corresponding classes:

    <p>CalAge = Calculates age from date of birth Comp1 = Returns the maximum of two integers Journal = Stores entries for research papers EgComp = Demonstrates the use of imported classes</p> Signup and view all the answers

    Which method in the CalAge class checks if the date of birth is valid?

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

    What are the four operations that should be defined in the calculator package?

    <p>Addition, Subtraction, Multiplication, Division</p> Signup and view all the answers

    The method sqrt() can be called using its class name when static import is used.

    <p>False</p> Signup and view all the answers

    Study Notes

    SWENG3101 Object Oriented Programming

    • This course covers Object Oriented Programming concepts at AASTU's Software Engineering Department.
    • The course structure includes Chapter 3: Classes and Objects.

    Chapter 3: Classes and Objects

    • Objectives: Describe objects and classes, use classes to model objects, access object data and methods, distinguish between instance and static variables/methods, create objects using constructors, and use the keyword 'this'.
    • Content: Classes and Objects, Access Control (private, protected, public), Attributes and Methods, and Constructors.
    • Objects: Entities in the real world, are tangible and intangible, and distinctly identifiable (e.g., student, room, account). Objects consist of data and operations that manipulate that data.
      • Example (Student): Data includes name, gender, birth date, address, phone number, and age; operations assign and change these data values.
      • Example (Dog): Data includes name, breed, color; operations include barking, wagging, running.
    • Class: A template or blueprint that defines the properties and behaviors of objects. Java classes use variables to define data fields and methods to define actions. Each object can have different data, but all objects of a class share the same data types and methods. A class functions as a mold or template dictating object capabilities.
      • Objects are instances of a class. Many instances of a class can be created. Creating an instance is called instantiation.
      • Example: A class called "Circle" can have instances "circle1", "circle2", "circle3", all sharing the same method 'getArea' but having different radii.

    Declaring and Creating Classes

    • Syntax: The format for declaring a class in Java. A class is a blueprint/template from which object are created
    • Creating Objects: How to create and instantiate objects from classes. The syntax involved in object creation.
      • Example Syntax: ClassName objectName = new ClassName(); (or with parameters)

    Accessing Data Members

    • Access data members and methods using the dot (.) operator.
      • Example: objectName.dataMember or objectName.methodName().

    Example Code (GradeBook, Circle)

    • Examples of defining classes and how to implement related functions/methods
      • GradeBook Example: Demonstrates a class for representing grade books with methods to show display messages.
      • Circle Example: A template/class used to create circles with methods like 'area()' and 'circumf()'.

    Declaring Variables (Local, Instance, Static)

    • Local Variables: Declared inside methods/constructors/blocks, visible only within that section, and destroyed when the method completes. No default values. Must be initialized before use.
    • Instance Variables: Declared inside a class but outside any method. Created when an object is created, destroyed when object is destroyed. Have default values (0 for numbers, false for booleans, null for object references).
    • Static Variables (Class Variables): Declared with the 'static' keyword inside a class and outside any method. Only one copy exists per class regardless of number of objects. Typically used as constants(rare). Created and destroyed with the program.

    Instance Variables (cont'd)

    • Instance variables have default values. Their values can be assigned during declaration or within a constructor. Access modifiers can be applied.

    Example Code (Local Variables, Static Variables)

    • Examples demonstrating local variables, their limitations, and static variables
    • Example 1: Implementing examples using pupAge() to illustrate the local scope of a variable
    • Example 2: Creating variable examples including methods, to access a class's instance/static variables.

    Example Code (BankAccount)

    • Demonstrating basic bank account functionality using variables.
    • Demonstrates deposit and withdrawal functions
    • Includes constructor initialization of the bank account balance.

    Example Code (Predicting Output)

    • Example with sample variable/method declarations to demonstrate the use of instance and static variables/methods and their scope.
      • Shows how to declare methods that accept parameters, how to call/define methods that take parameters.

    Methods Explained

    • Defining Methods: The structure/syntax of a method declaration. Includes return type and parameters
    • Parameters & Arguments: Explained together to understand parameter passing.
      • Arguments are the values passed to a method's parameters when the method is called.
    • Return Statements: To understand that a method that return a value must include a return statement.
    • Example Codes (Circle, GradeBook2): These examples demonstrate the use of methods with parameters and return values

    Constructors

    • Special methods that are fundamental to instantiating/creating new objects of an object from a class definition.
    • Purpose: To initialize instance variables of a class when creating an object.
      • Includes constructor format, no-argument/no-arg constructor format
    • Example Codes(Circle Constructor, Employee, Account): demonstrate various types of constructor implementation.

    Accessors and Mutators

    • Accessors: Public methods to access private instance variables.
    • Mutators: Public methods to change the values of a variable. Used to set private class-level variables' values.

    The this Keyword

    • Purpose: Use to resolve naming conflicts between local variables and instance variables, or call another constructor.
    • Example Codes: Detailed example uses of this keyword to demonstrate its use to refer to current object.

    Packages

    • Purpose: Organize related classes, avoid naming conflicts, enhance reusability.
      • Include built-in packages (lang, util), user-defined packages.
    • Structure example: A diagram showing different structures of Java packages.

    Subpackages

    • How subpackages are organized. Subpackages are nested packages within a package. Explains explicitly importing relevant sub-packages.
    • Example Code (importing specific packages/subpackages): Explains the syntax for importing relevant sub-packages to use in any code.

    Static Import

    • Purpose: Directly access static members of a class without using the class name.
    • Syntax: import static packageName.ClassName.*;

    Exercises (Included)

    • Sample/Illustrative exercises on Object-Oriented Programming
      • Demonstrates different ways of using relevant concepts

    Additional Information/Topics

    • Wrapper Classes, JAR files (Creating). These are not comprehensively covered in this summary.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    This quiz covers the foundational concepts of Classes and Objects in Object Oriented Programming, tailored for AASTU's Software Engineering Department. You'll explore access control, attributes, methods, and constructors, as well as practical examples like students and dogs. Test your knowledge and understanding of the essential aspects of OOP.

    More Like This

    Use Quizgecko on...
    Browser
    Browser