01:198:111 Lecture Notes - Lecture 14: Default Constructor
01:198:111 verified notes
14/28View all
❖ 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
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.