01:198:111 Lecture Notes - Lecture 14: Default Constructor

70 views2 pages
Verified Note
Review
This code does not compile. Why?
Do{
int prompt = IO.readInt();
System.out.println(prompt);
} while(prompt != -999);
The scope does not have prompt initialized
This means because int prompt was initialized inside the do while
loop, when you exit it, that variable stop existing and thus causes
a compile error
Provide some values of fnum, snum, and termnum so that above do while loop
terminates
Do{
max=Math.max(fnum,snum);
smax=snum
fnum=IO.readDouble();
snum=IO.readDouble();
}while(fnum != termnum || snum != termnum);
Whenever fnum or snum is equal to the termnum
Why is this a bad function design?
public static int square(){
int n=IO.readInt();
int sqr = n*n;
return sqr;
}
You don’t want to do inputs/IO functions within a function
public static int square(int n){
int sqr = n*n
return sqr;
This is better because the input is taken from outside the function
OOP
Goal: Simulate a real world using virtual objects
Each virtual object:
Is in instance of a class template
Has its own state(instance variables)
Shares a common set of behaviors with other objects of the same type
Once a class is defined
It can be considered a custom defined data type
Objects of that type can be created
Core concepts:
Defining a class
Instantiation of an object
Default constructor
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The scope does not have prompt initialized. This means because int prompt was initialized inside the do while loop, when you exit it, that variable stop existing and thus causes a compile error. Provide some values of fnum, snum, and termnum so that above do while loop terminates. Whenever fnum or snum is equal to the termnum. Public static int square(){ int n=io. readint(); int sqr = n*n; return sqr; You don"t want to do inputs/io functions within a function. Public static int square(int n){ int sqr = n*n return sqr; This is better because the input is taken from outside the function. Goal: simulate a real world using virtual objects. Shares a common set of behaviors with other objects of the same type. It can be considered a custom defined data type. Objects of that type can be created. Defining a class means you are giving it a name for which objects can be instantiated from as a template.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents