first commit
This commit is contained in:
8
CS1322/t2/Test1.java
Normal file
8
CS1322/t2/Test1.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Test1{
|
||||
public Test1(){
|
||||
System.out.println("test1()");
|
||||
}
|
||||
public Test1(String name){
|
||||
System.out.println("test1("+name+")");
|
||||
}
|
||||
}
|
||||
63
CS1322/t2/Test2.java
Normal file
63
CS1322/t2/Test2.java
Normal file
@@ -0,0 +1,63 @@
|
||||
public class Test2 extends Test1{
|
||||
public String name;
|
||||
public Test2(){
|
||||
this("bob");
|
||||
}
|
||||
public Test2(String name){
|
||||
System.out.println("Test2(name)");
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String reverse(String s){
|
||||
String bwd;
|
||||
if(s.length() != 1){
|
||||
bwd = reverse(s.substring(1)) + s.substring(0,1);
|
||||
}
|
||||
else {
|
||||
bwd = s;
|
||||
}
|
||||
return bwd;
|
||||
}
|
||||
|
||||
public boolean isPalindrome(String s){
|
||||
String bwd = reverse(s);
|
||||
if(s.equals(bwd)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void printTwoSquares(int a, int b){
|
||||
int iA=0,iB=0,rA=0,rB=0;
|
||||
char print = '#';
|
||||
while(rA<a || rB<b){
|
||||
if(rA >= a){
|
||||
print = ' ';
|
||||
}
|
||||
for(iA=0;iA<a;iA++){
|
||||
System.out.print(print);
|
||||
}
|
||||
System.out.print(" ");
|
||||
for(iB=0;iB<b;iB++){
|
||||
System.out.print("#");
|
||||
}
|
||||
System.out.println();
|
||||
rA++;
|
||||
rB++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]){
|
||||
Test2 bob = new Test2();
|
||||
|
||||
System.out.println(bob.reverse("Trent Reznor"));
|
||||
/*String s = bob.reverse("Tom the Eal");
|
||||
System.out.println(s);
|
||||
|
||||
System.out.println(bob.isPalindrome("poop"));
|
||||
System.out.println(bob.isPalindrome("vegetta"));
|
||||
bob.printTwoSquares(5,3);*/
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
73
CS1322/t2/t2 pract.txt
Normal file
73
CS1322/t2/t2 pract.txt
Normal file
@@ -0,0 +1,73 @@
|
||||
Review questions for test 2:
|
||||
|
||||
1) Define the following words:
|
||||
|
||||
- polymorphism
|
||||
- inheritance
|
||||
- dynamic binding
|
||||
- scope
|
||||
- variable shadowing
|
||||
- this()
|
||||
- super()
|
||||
- constructor chaining
|
||||
- toString
|
||||
- .equals
|
||||
- reference
|
||||
- object
|
||||
- class
|
||||
|
||||
|
||||
2) Write the method
|
||||
public String reverse(String s)
|
||||
which returns the String backwards.
|
||||
You may assume that s is not null.
|
||||
You must use recursion ONLY.
|
||||
|
||||
{
|
||||
|
||||
|
||||
|
||||
3) Write the method
|
||||
public boolean isPalindrome(String s)
|
||||
which returns true if and only if s is a palindrome, false otherwise.
|
||||
You may assume that s is not null.
|
||||
You must use recursion ONLY.
|
||||
Remember that a palindrome is a String that is the same forwards as
|
||||
backwards.
|
||||
|
||||
4) Write the method
|
||||
public void printTwoSquares(int a, int b)
|
||||
which prints out two squares of # marks, one of size a, one
|
||||
of size b, that are next to each other.
|
||||
For example,
|
||||
|
||||
printTwoSquares(3,5)
|
||||
would print
|
||||
|
||||
### #####
|
||||
### #####
|
||||
### #####
|
||||
#####
|
||||
#####
|
||||
|
||||
5) Given the following class:
|
||||
public class Clock
|
||||
{
|
||||
int time;
|
||||
public Clock(int time)
|
||||
{
|
||||
this.time=time;
|
||||
}
|
||||
}
|
||||
Write the class AlarmClock, which
|
||||
o is a subclass of Clock
|
||||
o has an int for alarmTime
|
||||
o has a constructor which takes both the alarmTime and the time,
|
||||
uses the super constructor to initalize time, and then intializes
|
||||
alarmTime
|
||||
o has a default constructor which constructor chains with 0 for
|
||||
the time and 0 for the alarm time
|
||||
o has an accessor and modifier for alarmTime
|
||||
|
||||
6) Do you understand all of the concepts and how to code them from p2 phase I?
|
||||
Just memorizing the code is *NOT* the way to go.
|
||||
Reference in New Issue
Block a user