Java Classes Flashcards
34 Questions
100 Views

Java Classes Flashcards

Created by
@PrettyAnaphora

Questions and Answers

What is a class?

It is a template that defines the form of an object. It defines the data (variables) and code that acts on the data.

What is an object?

An instance (copy) of a class

What are the two basic parts of a class?

Instance variables and methods

What is another name for the code contained within a class?

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

What are members of a class?

<p>The instance variables and methods</p> Signup and view all the answers

What are instance variables?

<p>The data members</p> Signup and view all the answers

What is a key point for a well-designed class?

<p>Should define one entity (concept)</p> Signup and view all the answers

What method is the starting point for any program?

<p>Main method</p> Signup and view all the answers

What is the general form of the dot operator?

<p>Object.member</p> Signup and view all the answers

Given the piece of code shown below, in which line(s) is (are) an object(s) created? 1 class Vehicle { 2 int passengers; 3 int fuelcap; 4 int mpg; 5 } 6 class VehicleDemo { 7 public static void main(String args[]) { 8 Vehicle minivan = new Vehicle(); 9 Vehicle truck = new Vehicle(); 10 int range;

<p>8 and 9</p> Signup and view all the answers

What are the names of the instance variables in this class?

<p>Passengers, fuelcap, mpg</p> Signup and view all the answers

Which lines represent the main program?

<p>Line 6 and onwards</p> Signup and view all the answers

Which lines represent the class?

<p>1 through 5</p> Signup and view all the answers

Should a class have many themes?

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

What's the name of the class in the code above that represents the main program?

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

How many classes are created when this program is compiled and what are the names of them?

<p>Two, Vehicle.class and VehicleDemo.class</p> Signup and view all the answers

How do the contents of the variables of minivan compare to the contents of the variables of truck?

<p>They are independent of each other</p> Signup and view all the answers

Write a piece of code that creates an instance of Vehicle called sportscar

<p>Vehicle sportscar = new Vehicle();</p> Signup and view all the answers

Does the class in the code above have a method associated with it?

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

Between which lines could you insert a method?

<p>4 and 5</p> Signup and view all the answers

Write a method called range that computes and prints out the range of the vehicle within the method itself.

<p>void range() { System.out.println(&quot;Range is &quot; + fuelcap * mpg); }</p> Signup and view all the answers

How many parameters does your method have?

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

What is the return type of the method you just wrote?

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

What two items are used to cause your method to end?

<p>Curly bracket or return</p> Signup and view all the answers

Write a piece of code that invokes the range method you wrote on minivan.

<p>minivan.range();</p> Signup and view all the answers

Where does execution resume after a call to a method?

<p>With the line of code following the call</p> Signup and view all the answers

What would be the return type of the square root function you used before?

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

What is the form of a piece of code to return a calculation from a method?

<p>return value;</p> Signup and view all the answers

Is the return type of a method important?

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

What is an argument? For the code below, give an example. What does the program do? class Fact { boolean isFact(int a, int b) { if( (b % a) == 0) return true; else return false; } } class IsFact { public static void main(String args[]) { Fact x = new Fact(); if(x.isFact(2, 20)) System.out.println("Yes it is"); if(x.isFact(3, 20)) System.out.println("this won't be displayed"); }}

<p>A value(s) passed to a method. 3, 20 Decides if the first number is a factor of the second</p> Signup and view all the answers

What is a parameter?

<p>The variable that receives the argument (numbers)</p> Signup and view all the answers

How many parameters does isFact have?

<p>Two, a and b</p> Signup and view all the answers

Do parameters within a method have to be of the same type?

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

What is a constructor and what is it used for?

<p>A constructor is a piece of code within a class. It must have the same name as the class, and it's used for setting initial conditions on variables or for start up conditions on a program.</p> Signup and view all the answers

Study Notes

Classes and Objects

  • A class serves as a blueprint for creating objects, defining data and methods.
  • An object is an instance or copy of a class.
  • Two fundamental components of a class are instance variables and methods.
  • Instance variables represent the data attributes of a class.

Class Members

  • Members of a class include its instance variables and methods, defining the state and behavior.
  • The code within a class that performs actions is known as a method.
  • A well-designed class encapsulates a single concept or entity.

Program Structure

  • The main method is the entry point for execution in a Java program.
  • Dot operator's general syntax is Object.member, used to access members of a class.

Code Examples

  • In a defined class, objects are created with the syntax: ClassName objectName = new ClassName();
  • Example variables in a class Vehicle include passengers, fuelcap, and mpg.
  • The snippet of code provided creates instances of the Vehicle class at lines 8 and 9.

Class Relationships

  • A class should focus solely on one theme to avoid complexity.
  • In the provided code, the class representing the main program is VehicleDemo.
  • Compiling the program results in the creation of two class files: Vehicle.class and VehicleDemo.class.

Independence of Objects

  • Each object, like minivan and truck, has its own state; their variable contents are independent.

Methods and Parameters

  • A method can be added between specified lines in the class to define behavior.
  • An example method range() calculates a vehicle's range based on its variables.
  • The range() method has no parameters and returns void.

Execution Flow

  • Execution resumes after a method call with the line following the method invocation.
  • A method can return a value using the syntax return value;, with return types like int, double, or boolean.

Arguments and Parameters

  • An argument is a value passed to a method, while a parameter is a variable defined in the method's signature that receives an argument.
  • The provided example uses 2 and 20 as arguments, determining if 2 is a factor of 20.

Constructors

  • Constructors are special methods with the same name as a class, responsible for initializing new objects and setting up initial conditions.

Studying That Suits You

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

Quiz Team

Description

Test your knowledge of Java classes with these flashcards. Each card covers essential concepts such as classes, objects, instance variables, and methods. Perfect for beginners looking to understand object-oriented programming.

Use Quizgecko on...
Browser
Browser