Lesson 1 -------- (not done) Lesson 2 -------- 1. String=text int=integer numbers double=double precision floating point number 2. String 3. double (1.414 etc.) 4. int 5. double p = (1.921E-16); 6. int i = 407; 7. String my_name = "enyc"; 8. int count; 9. bankBalance = 136.05; 10. scooter13, doubled 11. c. e. 12. double dist = 1003; ok int alt = 1493.86 error -- noninteger number Lesson 3 -------- 1. public static void main(String args[]) { String s = "The number of rabbits is"; int argh = 129; String report = s + " " + argh + "."; System.out.println(report); } 2. "GROOVY DUDE" +\n 3. public static void main(String args[]) { String g = "Computer Science is for nerds"; System.out.println(g.toUpperCase()); } 4. "Gettysburg address" 5. "ore a" 6. count = 15 7. public static void main(String args[]) { String m = "Look here!"; System.out.println("\""+m+"\" has "+m.length()+" characters"); } 8. System.out.println("All \"good\" men should come to the aid of their country."); 9. System.out.println("Hello\nHello again"); 10. System.out.println("A backslash looks like this \\, ...right?"); 11. "e Haskel" 12. "n" Project... Name that Celebrity ------------------------------ public class main { public static void main(String args[]) { String s1 = "Allan Alda"; String s2 = "John Wayne"; String s3 = "Gregory Peck"; System.out.println(s1+">>>"+cut23(s1)); System.out.println(s2+">>>"+cut23(s2)); System.out.println(s3+">>>"+cut23(s3)); } private static String cut23(String input) { int length=input.length(); return input.substring(2,length-3); } } Lesson 4 ------- my experiment ------------- public class Tester { public static void main(String args[]) { int m=91, y=25; // m+=1; int before = m; int whilee = ++m; // int whilee = m++; int after = m; System.out.println("----"); System.out.println(before); System.out.println(whilee); System.out.println(after); } } note to self: "while" seems to be a reserved word. exercise on lesson 4 -------------------- 1. 109 104 2. j+ = 1; j++; ++j; 3. 1992.37 4. System.out.println(zulu--); 5. 3 6. v- = p-30 7. v = v-(p-30) 8. -22 9. 2 5 10. will not compile -- attempting to assign an integer to a value. 11. double m=3.14 b=3.14 f=3.14; 12. int x y z; 13. "7" (/ is doing integer result due to integer inputs.) 14. nb: this appears to do integer arithmetic due to all inputs being integer values. Note to self: integer divide truncates rather than rounds result -- modulus used to find remainder!! Note to self: break down debugging the result to find out what is going on! System.out.println( "-----" ); System.out.println( 3/4+5*2/33-3+8*3 ); System.out.println( 3/4+10/33-3+24 ); System.out.println( 0 + 0 -3 + 24 ); System.out.println( -3 + 24 ); System.out.println( 21 ); Note to self: problems with - character can be a problem! -- might be treated as negative number or treated as minus and might depend on spacing? 15. = 16. k = i%j; 17. 4 18. j=j-1; j--; --j; Project... Cheating on Your Arithmetic Assignment ------------------------------------------------- public class Tester { public static void main(String args[]) { int x = 0; System.out.println("-----"); System.out.println("79 + 3 * (4 + 82 - 68) -7 + 19 = "+(79 + 3 * (4 + 82 - 68) -7 + 19)); System.out.println("179 + 21 + 10) / 7 + 181 = "+((179 + 21 + 10) / 7 + 181)); System.out.println("10389 * 56 * 11 + 2246 = "+(10389 * 56 * 11 + 2246)); } } Lesson 5 -------- 1. final double E 2.718; 2. final int NUM_STUDENTS=236; 3. constants must be defined when declared. 4. should fail to compile 5. "78" 6. yes 7. add "(double)" before the 61/3 8. "10.0" nb: result being printed as floating point hence the display of ".0" 9. "17.5" nb: entering a ".0" on a number appears to cause the number to be interpreted as float hence causing the calculation result to be float too. 10. (3 + 3 * 10.3 - 3 * 5) 3 + 3 * 7.3 * 5 "112.5" shomn as output. nb: cast float as int appears to truncate rather than round. 11. *** not done 12. 3 13. 0 14. 4 15. 1 16. int j = (int)d / i test code:- public static void main(String args[]) { double d = 18.3; int i = 4; int j = (int)d / i; System.out.println( j ); } 17. no lesson 6 -------- my experiment:- --------------- public static void main(String args[]) { System.out.println( "-----" ); double a = 3.5; System.out.println( "a "+ a ); System.out.println( "floor a "+ Math.floor(a) ); System.out.println( "ceil a "+ Math.ceil(a) ); System.out.println( "round a "+ Math.round(a) ); System.out.println( "abs a "+ Math.abs(a) ); System.out.println( "(int)a "+ (int)a ); double b = -7.5; System.out.println( "b "+ b ); System.out.println( "floor b "+ Math.floor(b) ); System.out.println( "ceil b "+ Math.ceil(b) ); System.out.println( "round b "+ Math.round(b) ); System.out.println( "abs b "+ Math.abs(b) ); System.out.println( "(int)b "+ (int)b ); } ----- a 3.5 floor a 3.0 ceil a 4.0 round a 4 abs a 3.5 (int)a 3 b -7.5 floor b -8.0 ceil b -7.0 round b -7 abs b 7.5 (int)b -7 1. double y = Math.sqrt(x); 2. int k = (j * Math.abs(m)); 3. no -- Math.abs() needs int-int or double-double. double k = Math.abs(-127.5); 4. System.out.println(Math.pow(2,1.5)); 5. -157 6. -158 7. 158 8. 157 9. -157 10. -157 11. 157 12. 158 13. System.out.println(Math.log(18)); 14. b = Math.PI * p; Project... Compute This. ------------------------ *** Not sure how to interpret the Pipe-symbols either side of cos ... acos does not appear to create the given answer! public static void Tester() { // initialise instance variables double d1 = 3 * Math.PI * Math.sin(Math.toRadians(187)) + Math.cos(Math.toRadians(122)); double d2 = Math.pow(14.72,3.801) + Math.log(72); System.out.println("d1 = "+d1+" This seems to be WRONG -- pipes around cos important?"); System.out.println("d2 = "+d2); } d1 = -1.6785107660514766 This seems to be WRONG -- pipes around cos important? d2 = 27496.988867001543 Lesson 7 -------- Project... Going in Circles --------------------------- import java.io.*; import java.util.*; public class RadiusOfCircle { public static void main() { System.out.print("What is the area? "); Scanner kbInputObject = new Scanner(System.in); double area = kbInputObject.nextDouble(); double radius = Math.sqrt(area/Math.PI); System.out.println("Radius of your circle is "+radius+"."); } } Project... What's My Name? -------------------------- import java.io.*; import java.util.*; public class FullName { public static void main() { Scanner kbInput = new Scanner(System.in); System.out.print("What is your first name? "); String firstName = kbInput.nextLine(); System.out.print("What is your last name? "); String lastName = kbInput.nextLine(); String fullName = firstName+" "+lastName; System.out.println("Your full name is "+fullName); } } Lesson 8 -------- 1. true 2. true 3. true 4. false 5. true 6. a b (!a && b) a b (a || !b) false false false false false true false true true false true false true false false true false true true true false true true true 7. b = p && q; 8. w = (x > 0) || (y == z); 9. true false 10. boolean kDog = a != b; 11. boolean kDog = !(a==b); 12. && 13. || 14. false 15. true 16. true 17. false 18. false Lesson 9 -------- 1. -- 10. *** not done 11. if ( m <= 44 ) b = true; 12. if ( r > 17 ) b = false; 13. false 14. "Not equal" 15. "Equal" 16. "200","20" 17. if ( g == 34 ) b = false; 18. boolean b = (k%2) == 0; 19. import java.io.*; import java.util.*; public class Tester { public static void main() { System.out.print("Enter your password"); Scanner kbI = new Scanner(System.in); String password = kbI.nextLine(); if (password.equals("XRay")) { System.out.println("Password entered succcessfully."); } else { System.out.println("Incorrect password."); } } } 20. "Two" Project... Even or Odd? ----------------------- *** not done Lesson 10 --------- nb: suspect that "break;" causes no further cases to be considered (i.e. leaves the switch statement). 1. char, int 2. 4 3. switch(myChar) { case 'g': case 'G': y++; break; case 'm': case 'M'; y--; break; default: y+=100; } 4. 3 5. char chr = 'z'; 6. "The sum is 1012" "The sum is 22" 7. switch(speed); { case 75: System.out.println("Exceeding speed limit"); break; case 69: case 70: System.out.println("Getting close"); break; case 65: System.out.println("Cruising"); break; default: System.out.println("Very slow"); } 8. no 9. char chr = (char)s; Project... Weight on Other Planets ---------------------------------- import java.io.*; import java.util.*; public class Tester { public static void main() { Scanner kbI = new Scanner(System.in); System.out.print("What is your weight on the earth? "); double weight = kbI.nextDouble(); System.out.println(""); System.out.println("1. Voltar"); System.out.println("2. krypton"); System.out.println("3. Fertos"); System.out.println("4. Servontos"); System.out.println(""); System.out.print("Selection? "); int selection = kbI.nextInt(); String planetName; switch(selection) { case 1: planetName = "Voltar"; weight*=0.091; break; case 2: planetName = "Krypton"; weight*=0.720; break; case 3: planetName = "Fertos"; weight*=0.865; break; case 4: planetName = "Servontos"; weight*=4.612; break; default: planetName = "Earth"; } System.out.println(""); System.out.println("Your weight on "+planetName+" would be "+weight); } } Lesson 11 --------- 1. 5 2. 7 3. 21 4. *** not done 5. *** not done 6. *** not done 7. for(int n=3; n!=48; n*=2 ) { System.out.println(n); } } 8. for(int n=24; n!=1; n/=2 ) { System.out.println(n); } } 9. 10. Control expression 11. j=5 j=10 j=15 j=20 j=25 j=30 j=35 j=40 j=45 j=50 12. 16 13. infinite loop 14. 137 Project... Name Reversal ------------------------ public class Tester { public static void main() { Scanner kbI = new Scanner(System.in); System.out.print("Please enter your name. "); String inName=kbI.nextLine(); String outName=""; int length=inName.length(); for (int x=length;x>0;x--) { outName=outName+inName.substring(x-1,x); } System.out.println(outName.toLowerCase()); } } lesson 12 --------- 1. while(control expression) { loop content } 2. do { loop content } while(control expression); 3. int m; m=97; while (m <= 195); { k=k*k+3*m; p=p+m+1; m++; } 4. int v=2 do { k=k*k+3*v; q=Math.sqrt(q+v+1); v*=3; } while (v <= 195); 5. !done 6. ; after do 7. infinite 8. infinite 9. extra ; after } (maybe not considered error): 'done' not defined here 10. "0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2 " 11. break; 12. while or for 13. 3 14. import java.io.*; import java.util.*; public class Tester { public static void main() { Scanner kbI = new Scanner(System.in); int userInput; do { System.out.print("Please enter an integer: "); userInput = kbI.nextInt(); System.out.println(Math.pow(userInput,2)); } while (userInput != 0); } } contest-type-problems --------------------- *** not done lesson 13 --------- 1. 0x41 (65) 2. 0x5A (90) 3. 0x61 (97) 4. 0x7A (122) 5. 26 6. 0x30 (48) 7. 0x39 (57) 8. displays 26 uppercase ASCII letters 9. displays string s in lowercase. 10. String b = ""+a; 11. char q = p.charAt(0); 12. no 13. yes 14. no 15. "69" 16. "E" 17. char cv = Character.toUppercase(boy); 18. char cv = Character.toLowercase(boy); 19. Character.isDigit(bv) 20. Character.isLetter(bv) 21. Character.isUpperCase(bv) 22. Character.isLetterOrDigit(bv) 23. Character.isLowerCase(bv) 24. prints all ASCII whitespace characters on separate lines. lesson 14 --------- Project... Basically Speaking ----------------------------- public class Tester { public static void main() { System.out.println("Decimal\tBinary\tOctal\tHex\tCharacter"); for (int x=0x41; x<0x5B; x++) { System.out.print(x+"\t"); System.out.print(Integer.toString(x,2)+"\t"); System.out.print(Integer.toString(x,8)+"\t"); System.out.print(Integer.toString(x,16)+"\t"); System.out.println((char)x); } } } lesson 15 --------- Project... What's That Diameter? // NB: extended this assignment a bit ;-) -------------------------------- // did overloaded method and test public class test { public static void main() { System.out.println(""); System.out.println("enyctest Circle class"); System.out.println("---------------------"); System.out.println(""); System.out.println("creating circle0 radius 3"); System.out.println("creating circle1 diameter 6"); System.out.println("creating circle2 radius 12"); System.out.println("creating circle3 diameter 24"); Circle circle0 = new Circle(3); Circle circle1 = new Circle(0,6); Circle circle2 = new Circle(12); Circle circle3 = new Circle(0,24); printCircleDetails(circle0,"circle0"); printCircleDetails(circle1,"circle1"); printCircleDetails(circle2,"circle2"); printCircleDetails(circle3,"circle3"); System.out.println("resetting circle0 to radius 12"); System.out.println("resetting circle1 to diameter 24"); circle0.setRadius(12); circle1.setDiameter(24); printCircleDetails(circle0,"circle0"); printCircleDetails(circle1,"circle1"); printCircleDetails(circle2,"circle2"); printCircleDetails(circle3,"circle3"); } public static void printCircleDetails(Circle circleToPrintDetailsOf, String nameOfCircle) { System.out.println("* Details of Circle apparently named \""+nameOfCircle+"\" :-"); System.out.print(" radius="+circleToPrintDetailsOf.radius()); System.out.print(" diameter="+circleToPrintDetailsOf.diameter()); System.out.print(" area="+circleToPrintDetailsOf.area()); System.out.println(" circumference="+circleToPrintDetailsOf.circumference()); } } public class Circle { public double radius; public double diameter; public Circle(double radiusConstructor) { radius = radiusConstructor; diameter = radius*2; } // enyc trying to overload a constructor (another // constructor method) -- providing to allow // circle to be defined by diameter by passing // 0, diameter instead. public Circle(int notused, double diameterConstructor) { diameter = diameterConstructor; radius = diameter/2; } // Seems to work!!!! public double area() { double a = Math.PI * Math.pow(radius,2); return a; } public double circumference() { double c = Math.PI * diameter; return c; } public double diameter() { double d = diameter; return d; } public double radius() { return radius; } public void setRadius(double radiusSet) { radius = radiusSet; diameter = radius*2; } public void setDiameter(double diameterSet) { diameter = diameterSet; radius = diameter/2; } } 1. *** not done [...] 20. *** not done Project... Overdrawn at the Bank -------------------------------- import java.io.*; import java.util.*; public class Tester { public static void main() { Scanner kbI = new Scanner(System.in); System.out.print("amount of money? "); String amtstr=kbI.nextLine(); int amt=Integer.parseInt(amtstr); System.out.print("Name? "); String name=kbI.nextLine(); BankAccount myAccount = new BankAccount(amt,name); myAccount.deposit(505.22); System.out.println(myAccount.balance); myAccount.withdraw(100); System.out.println("The "+myAccount.name+" account balance is, $"+myAccount.balance); // System.out.println(amt); // System.out.println(name); } } public class BankAccount { public double balance; // These need to be public to allow direct public String name; // access e.g. myAccount.balance public BankAccount(double b, String n) { balance=b; name=n; } public void deposit(double d) { balance+=d; } public void withdraw(double w) { balance-=w; } } lesson 16 --------- 1. true 2. false 3. false 4. false 5. false 6. no -- trying to store String in int 7. calling a private method 8. yes 9. incorrect number of parameters for method 10. no -- trying to reference private variable 11. yes 12. Surfer surferDude; surferDude = new Surfer; 13. c. 14. 2505 15. declaring an object but not instantiating it. 16. first line incorrectly formatted. second line may or may not work depending if first line assumed to instantiate object ok. third line calling method with string rather than integer. Project... Gas Mileage ---------------------- public class Tester { public static void main() { Automobile myBmw = new Automobile(24); myBmw.fillUp(20); myBmw.takeTrip(100); double fuel_left = myBmw.reportFuel(); System.out.println(fuel_left); } } public class Automobile { private double mpg; private double gallons; public Automobile(double mpgConstructor) { mpg = mpgConstructor; gallons = 0; } public void fillUp(double fuelAdded) { gallons+=fuelAdded; } public void takeTrip(double milesDriven) { gallons-=(milesDriven/mpg); } public double reportFuel() { return gallons; } } lesson 17 --------- Project... "AddemUp" -------------------- import java.io.*; import java.util.*; public class AddemUp { public static void main() { System.out.println("------------------------------"); System.out.println("NB: this program runs left-right sum of negative or positive integers."); Scanner kbI = new Scanner(System.in); String userInput; do { System.out.print("Enter Something like \"8 + 33+ 1,345 -137\": "); userInput = kbI.nextLine().trim(); //System.out.println("trimmed user input is: \""+userInput+"\""); Scanner inputScanner = new Scanner(userInput); inputScanner.useDelimiter("\\s*\\+\\s*"); // delimeter is (0 or more whitespace),"+",(0 or more whitespace) int newnumber; String newstring; int total = 0; while( inputScanner.hasNext() ) { newstring=inputScanner.next(); //System.out.println("newstring is: "+newstring); total+=dealWithPlusSeparatedParts(newstring.trim()); //System.out.println("total so far is: "+total); } System.out.println("Sum is: "+total); } while (userInput.length() > 0); } private static int dealWithPlusSeparatedParts(String inputString) { // NB: possible to re-use variable names in this method // from the main method as they seem to be independant. if ( inputString.length()==0 ) return 0; int initialMultiplier=-1; int newNumber; int totalNumber=0; if ( inputString.charAt(0)=='-' ) initialMultiplier=1; Scanner inputScanner=new Scanner (inputString); inputScanner.useDelimiter("\\s*-\\s*"); while( inputScanner.hasNextInt() ) { newNumber=inputScanner.nextInt(); //System.out.println("dbg: "+newNumber); totalNumber-=(newNumber*initialMultiplier); initialMultiplier=1; //System.out.println("tot: "+totalNumber); } return totalNumber; } } 1. "1" 2. "14" 3. "2" 4. "Lucky hpckey puck" 5. "1" 6. "o" 7. "1" 8. "14" 9. "11" 10. "k" 11. "-1" 12. "10" 13. "6" 14. "11" 15. "LuckA hockeA puck" 16. "true" 17. "1" 18. "-1" (or other negative int) 19. "0" 20. "1" (or other positive int) 21. ">>>Gu daay, mates<<<" 22. "Good" 23. "rning," 24. " how may" 25. "true" "p you?" 26. error ("I289" is not an int). Project... Encryption/Decryption -------------------------------- import java.io.*; import java.util.*; public class Tester { public static void main(String args[]) { Scanner kbReader = new Scanner(System.in); System.out.print("Enter a sentence that is to be encrypted: "); String sntnc = kbReader.nextLine( ); System.out.println("Original sentence = " + sntnc); Crypto myCryptObj = new Crypto( ); String encryptdSntnc = myCryptObj.encrypt(sntnc); System.out.println("Encrypted sentence = " + encryptdSntnc); String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); System.out.println("Decrypted sentence = " + decryptdSntnc); } } import java.util.*; public class Crypto { public void Crypto() { } public String encrypt(String inputString) { String outputString=""; Scanner inputScanner = new Scanner(inputString); inputScanner.useDelimiter(""); while (inputScanner.hasNext()) { char a=inputScanner.next().charAt(0); switch (a) { case 'v': case 'V': outputString+="ag\',r"; break; case 'm': case 'M': outputString+="ssad"; break; case 'g': case 'G': outputString+="jeb..w"; break; case 'b': case 'B': outputString+="dug>?/"; break; default: outputString+=a; } } //encryptString=encryptString.replace("v","ag\',r"); // creates g //encryptString=encryptString.replace("V","ag\',r"); //encryptString=encryptString.replace("m","ssad"); //encryptString=encryptString.replace("M","ssad"); //encryptString=encryptString.replace("g","jeb..w"); // creates b //encryptString=encryptString.replace("G","jeb..w"); //encryptString=encryptString.replace("b","dug>?/"); // creates g //encryptString=encryptString.replace("B","dug>?/"); return outputString; } public String decrypt(String decryptString) { decryptString=decryptString.replace("ag\',r","v"); decryptString=decryptString.replace("ssad","m"); decryptString=decryptString.replace("jeb..w","g"); decryptString=decryptString.replace("dug>?/","b"); return decryptString; } } Lesson 18 --------- 1. double sgt[] = new double[800]; 2. "21" 3. for (int x=0; x>>123" 15. int indx; for (indx=0;indx