"Predicate" methods
(Methods/services/commands that have a return value) |
Note: Please keep the programs that you create
today, in case you have a dispute about your grades for the ICEs at the end of
the quarter. When you're working with a partner, each person should save their
own, individual copy
Part 1: Predicate Methods
For each of the following examples, write out exactly what is printed when the
program is run.
You must do this for each program, even
if it does not compile - in that case, make sure to explain what the program
WOULD do, if it was run (despite the compile errors)
1.
class PrintHelper extends Object
{
public int firstService()
{
return 1;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
int num;
num = ph.firstService();
System.out.println( num );
}
}
|
2.
class PrintHelper extends Object
{
public double service2()
{
return 3.14 + 1;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
double num;
num = ph.service2();
System.out.println(num);
}
} |
3.
class
PrintHelper extends Object
{
public double methodThree()
{
double temp = 3.14 + 1;
return 1;
System.out.println("Hi");
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
System.out.println(ph.methodThree());
}
} |
4.
class
PrintHelper extends Object
{
public double svc4( double
num)
{
while(num < 3)
{
num = num * 2;
}
return num;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
System.out.println( ph.svc4(2) + ph.svc4(3));
}
} |
5.
class
PrintHelper extends Object
{
public double
method5(double num)
{
System.out.println( "Will
this Print?");
System.out.println( "What about this?");
return num;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
System.out.println(ph.method5(2));
}
} |
6.
class
PrintHelper extends Object
{
public double svc6(double
num)
{
double temp = num + 2;
if( temp > 0)
return temp;
System.out.println( "Will this Print?");
System.out.println( "What about this?");
return 1.0;
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
System.out.println(ph.svc6(2));
}
} |
7.
class
PrintHelper extends Object
{
public boolean svc7(double
num)
{
if( num >= 0 )
{
return true;
}
else
{
return false;
}
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
boolean b;
b = ph.svc7(-7.76);
if( !b )
{
System.out.println( "Number is valid");
}
else
{
System.out.println( "Number is so negative, it's not funny");
}
}
} |
8.
class
PrintHelper extends Object
{
public boolean
methodEight(double num)
{
if( num >= 0 )
{
return true;
}
else
{
return false;
}
System.out.println( "Will this Print?");
}
}
public class FileName extends Object
{
public static void main(String[] args)
{
PrintHelper ph = new PrintHelper();
if(ph.methodEight(7.76) )
{ System.out.println( "Number is valid");
}
else
{
System.out.println( "Number is so negative, it's not funny");
}
}
} |
Part 2: Tracing a Predicate Method
After watching the instructor's demo, download the Java program named
FileName.java, and trace through it, paying special attention to the return
statement.
Part 3: Creating Predicate Services
Download the file ICE_14_Part_1_Test.java,
and write the predicate service called
isNorthOfStreet(int st)
This tests whether a robot is north of the street indicated by the parameter st.
The method should return true if the robot is north of the street, and false if
it isn't.
You should make sure to test your
new command by creating the robot on, say, street 2, facing north. You should
then ask if the robot is north of street 1(it isn't), if it's north of street 3
(it should be), and if it's north of street 2 (it isn't).
Once you've done that, create a
second, similar service to tell you if the robot is south of a given street:
isSouthOfStreet(int st);
Make sure to test your new robot out by trying
it out in various ways. For example, you might put it on street 5, and try
calling isNorthOfStreet(10),
isNorthOfStreet(0), and
isNorthOfStreet(5).