Java Classes
Write a method called range that computes and prints out the range of the vehicle within the method itself.
Void range() { System.out.println("Range is " + fuelcap * mpg); }
You cannot begin variable names with what?
A number . Ex) 5data
What is an overloaded method?
Many methods of the same names but of different return types and parameter lists.
What is another name for the code contained within a class?
Method
Which lines represent the class?
1 through 5
Between which lines could you insert a method?
4 and 5
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;
8 and 9
What is a constructor and what is it used for?
A constructor is a pieces 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.
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"); } }
A value(s) passed to a method. 3, 20 Decides if the first number is a factor of the second
What is an object?
An instance (copy) of a class
What two items are used to cause your method to end?
Curly bracket or return
What would be the return type of the square root function you used before?
Double
What are the two basic parts of a class?
Instance variables and methods
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.
Which lines represent the main program?
Line 6 and onwards
What does the dot operator do?
Links the name of an object with the name of a member.
What method is the starting point for any program?
Main method
The switch statement in Java is efficient for what?
Making selections.
Do parameters within a method have to be of the same type?
No
Should a class have many themes?
No
What is a key point for a well designed class?
Should define one entity (concept)
What are instance variables?
The data members
What are members of a class?
The instance variables and methods
The method of a class are what?
The subroutine.
What is a parameter?
The variable that receives the argument (numbers).
How do the contents of the variables of minivan compare to the contents of the variables of truck?
They are independent of each other
What's the name of the class in the code above that represents the main program?
VehicleDemo
What is the return type of the method you just wrote?
Void
Where does execution resume after a call to a method?
With the line of code following the call
Is the return type of a method important?
Yes, boolean, int, and double can not mix
Write a pieces of code that invokes the range method you wrote on minivan.
minivan.range();
What is the form of a piece of code to return a calculation from a method?
return value;
Does the class in the code above have a method associated with it?
No
How many parameters does your method have?
None
What does OOP stand for?
Object Oriented Programming.
What is the general form of the dot operator?
Object.member
What are the names of the instance variables in this class?
Passengers, fuelcap, mpg
The word static can be used to do what?
To get a subroutine to work on its own within your program.
How many classes are created when this program is compiled and what are the names of them?
Two, Vehicle.class and VehicleDemo.class
How many parameters does isFact have?
Two, a and b
Write a piece of code that creates an instance of Vehicle called sportscar
Vehicle sportscar = new Vehicle();