Commit 66c031e2 by Joe Student

Revert "Revert "cleared-out todays lab classes""

This reverts commit 912b8a35.
parent 912b8a35
......@@ -9,85 +9,6 @@ public class ArrayListManipulation {
*/
public static void main(String[] args) {
//delcare a new arrayList and import from java.util
//here we're using the generic form so we're tellign the compiler that
//this data structure can ONLY hold strings
ArrayList<String> strNames = new ArrayList<String>();
//the nice thing about arrayLists is that their length is indeterminate
//this type of data structure is known as a vector; it can grow and shrink -- very useful
strNames.add("Carol");
strNames.add("Marcia");
strNames.add("Jan");
strNames.add("Cindy");
//let's iterate over this ArrayList
for (String str : strNames) {
System.out.println(str);
}
System.out.println("-----------------------------");
String[] strMikesFamilyMembers = {"Mike", "Gregg", "Peter", "Bobby"};
//let's add them to our arraylist
//start at position zero
//traverse the String[] and insert at even positions
for (int nC = 0; nC < strMikesFamilyMembers.length; nC++) {
strNames.add(nC * 2, strMikesFamilyMembers[nC]);
}
//let's iterate over this ArrayList
for (String str : strNames) {
System.out.println(str);
}
System.out.println("-----------------------------");
strNames.add("Alice");
strNames.add("Tiger");
//wait...get ride of Tiger, Jan is allergic.
strNames.remove(strNames.size()-1);
//let's iterate over this ArrayList
for (String str : strNames) {
System.out.println(str);
}
System.out.println("-----------------------------");
//Using ArrayLists without Generics
//This ArrayList can take any object
ArrayList objObjects = new ArrayList();
objObjects.add(new Integer(5));
objObjects.add(new Double(5.5));
objObjects.add(new Boolean(true));
objObjects.add(new java.awt.Rectangle(1,5,15,78));
for (Object obj : objObjects) {
System.out.println(obj.toString());
}
System.out.println("-----------------------------");
//let's use the fully qualified Class name; rather than import; just for fun
ArrayList<java.awt.Rectangle> recShapes = new ArrayList<java.awt.Rectangle>();
recShapes.add(new java.awt.Rectangle(2,3,4,5));
recShapes.add(new java.awt.Rectangle(16,8,9,12));
recShapes.add(new java.awt.Rectangle(14,8,15,11));
for (java.awt.Rectangle rec : recShapes) {
System.out.println(rec.getSize().toString());
}
}//end main
......
......@@ -10,149 +10,6 @@ public class ArrayManipulation {
public static void main(String[] args) {
//this is an array of bytes
byte[] yAges = new byte[5];
//the indices are from 0 to 4
//values are initialzed to zero
System.out.println("printing values stored in yAges");
for(int nC = 0; nC < yAges.length; nC++) {
System.out.println(yAges[nC]);
}
System.out.println("-----------------------");
int[] nIds = {5874, 5698, 9981, 1012};
//this is an array of ints initialzied to above values
//the indices are from 0 to 3
System.out.println("printing values stored in nIds");
for (int nC = 0; nC < nIds.length; nC++) {
System.out.println(nC + ":" + nIds[nC]);
}
System.out.println("-----------------------");
//nResult is now assigned to 1012;
int nResult = nIds[3];
System.out.println("printing value stored in nIds[3]");
System.out.println(nResult);
System.out.println("-----------------------");
//will throw array index out of bounds error at runtime
//this will NOT complain at compiletime; you are responsible for
//keeping track of how many elements in your array
//int nResult2 = nIds[4];
System.out.println("-----------------------");
String[] strNames = {"Harry", "Larry", "Mary", "Perry"};
//this has indices from 0 to 3
//traditional way to traverse an array using counter
for(int nC = 0; nC < strNames.length; nC++){
System.out.println(nC + ":" + strNames[nC]);
}
System.out.println("-----------------------");
//since Java 5; you can use foreach -- no counters
//this reads, foreach String element called str in strNames
for (String str : strNames){
System.out.println(str);
}
Rectangle[] recShapes =
{
new Rectangle(4,5,8,9),
new Rectangle(7,1,9,12),
new Rectangle(8,16,1,3)
};
for (int nC = 0; nC < recShapes.length; nC++) {
System.out.println(nC + ":" + recShapes[nC]);
}
System.out.println("-----------------------");
for (Rectangle rectangle : recShapes){
System.out.println(rectangle);
}
//we've just created an array with 2 elements
//these elements have indices 0 and 1
//each points to null
Rectangle[] recBoxes = new Rectangle[2];
//iterate over these and print results
for (Rectangle rectangle : recBoxes) {
System.out.println(rectangle);
}
Random ran = new Random();
//assign values to them
for(int nC = 0; nC < recBoxes.length; nC++){
int nRan = ran.nextInt(10) + 1; //value from 1 to 10
recBoxes[nC] = new Rectangle(nC, nC, nRan, nRan);
}
//iterate over these and print results
for (Rectangle rectangle : recBoxes) {
System.out.println(rectangle);
}
int[][] nNumbers = new int[3][4];
for (int nRow = 0; nRow < nNumbers.length; nRow++) {
for (int nCol = 0; nCol < nNumbers[0].length; nCol++) {
nNumbers[nRow][nCol] = ran.nextInt(100);
}
}
for (int nRow = 0; nRow < nNumbers.length; nRow++) {
for (int nCol = 0; nCol < nNumbers[0].length; nCol++) {
System.out.print(nNumbers[nRow][nCol] + " : ");
}
System.out.println();
}
//ragged array
boolean[][] bAnswers = {
new boolean[6],
new boolean[9],
new boolean[3],
new boolean[8]
};
for (int nRow = 0; nRow < bAnswers.length; nRow++) {
for (int nCol = 0; nCol < bAnswers[nRow].length; nCol++) {
bAnswers[nRow][nCol] = ran.nextBoolean();
}
}
for (int nRow = 0; nRow < bAnswers.length; nRow++) {
for (int nCol = 0; nCol < bAnswers[nRow].length; nCol++) {
System.out.print(bAnswers[nRow][nCol] + " : ");
}
System.out.println();
}
}
......
......@@ -9,15 +9,6 @@ package lec02.glab;
*/
public class Convert {
public static double tempToMetric(double dFar){
//(°F - 32) x 5/9
return (dFar -32) * 5.0/9.0;
}
public static double tempToImperial(double dCel){
// °C x 9/5 + 32
return dCel * 9.0/5.0 + 32;
}
}
......@@ -13,59 +13,9 @@ public class NewKeyword {
public static void main(String[] args) {
Student stuOne = new Student("Alex", 2016);
Student stuTwo = new Student("Bart", 2017);
System.out.println("-----------original----------");
System.out.println("stuOne: " + stuOne);
System.out.println("stuTwo: " + stuTwo);
//swap
Student stuTemp = stuOne;
stuOne = stuTwo;
stuTwo = stuTemp;
System.out.println("-----------swaped----------");
System.out.println("stuOne: " + stuOne);
System.out.println("stuTwo: " + stuTwo);
System.out.println("---------call method swap------------");
swap(stuOne, stuTwo);
System.out.println("stuOne: " + stuOne);
System.out.println("stuTwo: " + stuTwo);
System.out.println("-----------swaped and incremented-----------");
//swap
stuTemp = stuOne;
stuOne = stuTwo;
stuTwo = stuTemp;
//increment
increment(stuOne);
increment(stuTwo);
System.out.println("stuOne: " + stuOne);
System.out.println("stuTwo: " + stuTwo);
}
public static void swap(Student stuFirst, Student stuSecond){
Student stuTemp = stuFirst;
stuFirst = stuSecond;
stuSecond = stuTemp;
}
public static void increment(Student stu){
stu.setGraduate(stu.getGraduate() + 10);
}
......
......@@ -13,31 +13,11 @@ public class PassBy {
public static void main(String[] args) {
int nNumber = 5;
multByFive(nNumber);
System.out.println("nNumber: " + nNumber);
Rectangle recSquare = new Rectangle(1,1,10,10);
doubleRec(recSquare);
System.out.println("recSquare: " + recSquare);
}
//when you pass primitives into methods in Java, the are passed by value, in other words - they are copied.
private static void multByFive(int nParam){
nParam = nParam * 5;
}
//when you pass an Object into a method, you are passing by reference, which means you're giving the method the
//memory address of the object
private static void doubleRec(Rectangle recParam){
recParam.setSize(recParam.width * 2, recParam.height * 2);
}
}
......@@ -15,17 +15,6 @@ public class RandomDriver {
public static void main(String[] args) {
Random random = new Random();
//will generate a number between 0-0.999999999
System.out.println(random.nextDouble());
//will multiply the generated double 0-0.9999) by the param
//then truncate it (not round)
// 99.999999 will be truncated to 99
// 0.99999 will be truncated to 0
//will generate a number between 0 and 99
System.out.println(random.nextInt(100));
}
......
......@@ -29,8 +29,9 @@ public class RandomNumbers {
System.out.println("--------------------------");
//roll the die 20 times
for (int nRoll = 0; nRoll < 20; nRoll++)
for (int nRoll = 0; nRoll < 20; nRoll++) {
System.out.print(" >" + yFacets[ran.nextInt(6)] );
}
......
......@@ -15,35 +15,6 @@ public class StaticDriver {
public static void main(String[] args) {
//non-static rectangle object which is the implicit parameter
Rectangle rectangle = new Rectangle(1,2,3,4);
System.out.println(rectangle.getCenterX());
//with static methods, there is more initial overhead. They need to be loaded in memory when the program starts up
//the compiler will only load those classes that are referenced in code
//In Java, you can recognize the static context immediately because
// 1/ We call the method from the Class e.g. Math.pow(), and 2/ there is NO implicit parameter
//why shouldn't you just create all methods this way, and never allocated memory on the heap using the new keyword
System.out.println(Convert.tempToImperial(100.0));
System.out.println(Math.pow(2,3));
ArrayList<Student> stuStudents = new ArrayList<>();
Student stu;
for (int nC = 0; nC < 100 ; nC++) {
stu = new Student(String.valueOf( Student.SCHOOL + " name: " + nC), nC);
stuStudents.add(stu);
}
System.out.println(Student.getInstanceNum());
......
......@@ -9,81 +9,6 @@ public class StringManipulation {
*/
public static void main(String[] args) {
//this is a String literal "Hello World";
//a String literal has no object reference, so unless it's assined to
//a reference or a passed into a method, and then assined, it's unfindable
//strResult refers to "Hello World." now.
String strResult1 = "Hello " + "World.";
System.out.println(strResult1);
String strOne = "CSPP";
String strTwo = strOne; //sometimes this will copy references, and sometimes values
//so you must assume values
String strState = "Mississippi";
System.out.println(strState);
strState = strState.replaceFirst("issipp", "our");
System.out.println(strState);
System.out.println(strState.length());
System.out.println("Pardon me " + strState.substring(0,4));
String strNeedsTrim = " some string ";
System.out.println(strNeedsTrim);
strNeedsTrim = strNeedsTrim.trim();
System.out.println(strNeedsTrim);
System.out.println(strOne.compareTo(strTwo));
System.out.println("Hello".compareTo("Hello"));
int nPos = strNeedsTrim.indexOf(' ');
System.out.println(nPos);
System.out.println(strState.endsWith("i"));
//string pools
String strBig0 = new String("Illinois");
String strBig1 = new String("Indiana");
String strBig2 = new String("Iowa");
String strBig3 = new String("Michigan");
String strBig4 = new String("Michigan State");
String strBig5 = new String("Minnesota");
String strBig6 = new String("Nebraska");
String strBig7 = new String("Northwestern");
String strBig8 = new String("Ohio State");
String strBig9 = "Pennsylvania";
String strBig10 = "Purdue";
String strBig11 = "Wisconsin";
String strNoLake0 = new String("Iowa");
String strNoLake1 = new String("Nebraska");
System.out.println(56.21 == 56.21);
//the == sign is comparing memory address
Rectangle recOne = new Rectangle(2,3,4,5);
Rectangle recTwo = new Rectangle(2,3,4,5);
//this is false
System.out.println(recOne == recTwo);
recOne = recTwo;
//this is true
System.out.println(recOne == recTwo);
//this should be false, but sometimes it's true??
System.out.println(strNoLake1 == strBig6);
}
......
......@@ -8,44 +8,5 @@ package lec02.glab;
* To change this template use File | Settings | File Templates.
*/
class Student{
private String mName;
private int mGraduate;
private static int sInstanceNum;
public static final String SCHOOL = "UCHICAGO";
Student(String name, int graduate) {
mName = name;
mGraduate = graduate;
sInstanceNum++;
}
int getGraduate() {
return mGraduate;
}
void setGraduate(int graduate) {
mGraduate = graduate;
}
private String getName() {
return mName;
}
private void setName(String name) {
mName = name;
}
public static int getInstanceNum() {
return sInstanceNum;
}
@Override
public String toString() {
return "Student{" +
"mName='" + mName + '\'' +
", mGraduate=" + mGraduate +
'}';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment