Loop Exercises

What output will be generated from the following:
public class MyLoop {
public static void main(String[] args) {
String[] sa = {“tom “, “jerry “, “Jonathan”};
for(int x = 0; x < 1; x++) {
for(String s: sa) {
System.out.print(x + " " + s);

}
}
}
}

public class MyLoop {
public static void main(String[] args) {
String[] sa = {"tom ", "jerry ", “Jonathan”};
for(int x = 0; x <=2; x++) {
for(String s: sa) {
System.out.print(x + " " + s);
if(x!=0) break;
}
}
}
}
public class MyLoop {
public static void main(String[] args) {
String[] sa = {"tom ", "jerry "};
for(int x = 0; x < 3; x++) {
for(String s: sa) {
System.out.print(x + " " + s);
if( x == 2) break;
}
}
}
}
public class MyLoop {
public static void main(String[] args) {
String[] sa = {"tom ", "jerry ", “Jonathan”};
for(int x = 0; x < 4; x++) {
for(String s: sa) {
if( x == 0){ x=2};
System.out.print(x + " " + s);

}
}
}
}

Exercise

Compare and Contrast the methods associated with the List Interface and The Array List Class
Highlight the methods in the ArrayList class and state the implications relating to method access when instantiating an ArrayList using the List Interface as a type as shown:
List myList = new ArrayList();

Debugger Exercise

public class Debugger {

public static void main(String[] args)
{
//        int[] a = {1,2,3,4,5};
//        int[] b = {1,2,3,4,5};
//
//        // should print 15
//        System.out.println(addArray(a));
//
//        int[] array = add(1,2,3);
//        // print out should be 1 2 3 6
//        for(int num : array)
//        {
//            System.out.print(num+ ” “);
//        }
//
//        // Should hand for user entering incorrect index
//        printArrayIndex(b, 5);
//        /// Out put should be 1 2 3 4 5
//        printArray(a);

// Call the Sum method should print 20 to the console
sum(10, 10, 10);
// Should print false
System.out.println(test(5,5));

// the value 10 should be printed to the console
assign(10);

}

//Method add up the contents of an int array and returns the result
public static int addArray(int[] ary)
{
int sum = 0;
for(int i=0;i<ary.length;i++)
{
sum += ary[i];
i++;
}
return sum;
}
// This method adds 3 ints and puts them into an array at index 0,1 and 2 and then
// stores the sum of the numbers at index 3 of the array
// the method returns the array with numbers in it
public static int[] add(int a, int b, int c)
{
int sum=a+b+c;
int[] array = new int[10];
for(int i=0;i<6;i++)
{
array[i]=a;
array[i+1]=b;
array[i+2]=c;
array[i+3]=sum;
}
return array;
}
// this method prints out the contents of an array
public static void printArray(int a[])
{

for(int i=0;i<a.length;i++)
{
System.out.print(a[i++] + ” “);

}
}
// this method prints out the element at a specified index
public static void printArrayIndex(int a[], int index)
{

System.out.print(a[index]);

}

// this method adds 2 integers and prints them
public static void sum(int a, int b, int sum)
{

sum += a+b;

printSum(sum);

}
public static void printSum(int a)
{

System.out.println(“The Sum is:”+ ++a);

}
// this method returns true is the multiplication of 2 integers is <20 otherwise returns false
public static boolean test(int a, int b)
{

if(a*b>20 || a*b <20)
return true;

return false;

}
// this method assign a value to the local variable b and prints the value to the console
public static void assign(int a)
{
int b=0;

System.out.println(b);

}

}

Command Line

Investigate compiling and running a java program from the command line.
Investigate passing arguments from the command and printing the arguments passed to the command line.

ArrayLists and the For Each Loop Exercise

Investigate the ArrayList Class and the For Each Loop construct. Use an Array list to store a collection of Animal Objects from our upcasting/downcasting exercise and investigate and demonstrate the use of the methods associated with the ArrayList class.
Use a For Each Loop construct to traverse an ArrayList.

Exam Exercises

Exam Exercises

• Investigate the valid ways and invalid ways for declaring and initialising 1 dimensional arrays, 2 dimensional and 3 dimensional arrays
• Investigate and demonstrate the use of all the methods associated with the String Class using the following String myString = “I Love Java Yes I Do!” . For example to demonstrate the use of the charAt() method I would do the following:
System.out.println(“The character at position 5 in the String “+myString+ “ is “+myString.charAt(5);
• Investigate and demonstrate the use of all the methods associated with the StringBuilder Class using the following StringBuilder myStringBuilder = new StringBuilder( “I Love Java Yes I Do!”); . For example to demonstrate the use of the deleteCharAt() method I would do the following:
StringBuilder myStringBuilder = new StringBuilder(“I Love Java Yes I Do”);
myStringBuilder.deleteCharAt(5);
System.out.println(myStringBuilder);

Octals as integer literals

http://www.java2s.com/Tutorial/Java/0040__Data-Type/Definingintegerliteralsasoctalvalues.htm

  1. Octal values is base 8
  2. Legal digits in an octal literal can be from 0 to 7
  3. Octal literals are with a leading zero, for example 035 and 067
public class MainClass{

public static void main(String[] a){
int octValue = 036;
System.out.println(octValue);
}

}

To change 036 octal to decimal: Keep the right number, multiply all other digits by the base number times itself the same number of times that it is to the left of your right number..
Right most number 6
Multiply 3 x 8 = 24
Add up numbers you get 30

067

Right most number is 7

Multiply 6 x 8 = 48

Add up numbers you get 55

014

Right most number is 4

Multiply 1 x 8 = 8

Add up numbers you get 12

601 decimal to octal
Divide 600 by 8 = 75 and the remainder is 1
Divide 75 by 8 = 9 and the remainder is 3
Divide 9 by 8 =1 and the remainder is 1
Divide 1 by 8 = 0 and the remainder is 1
Reading from bottom to top 300 decimal is 1131 octal.
So keep dividing the number by the base number, until you only have a remainder, then use last remainder as the most significant number..

To change 1131 octal to decimal: Keep the right number, multiply all other digits by the base number times itself the same number of times that it is to the left of your right number..
Right most number 1
Multiply 3 x 8 = 24
Multiply 1 x 8 x 8 = 64
Multiply 1 x 8 x 8 x 8 = 512.
Add your numbers up and you get 601.

Valid and invalid for loop declarations

// Valid
for(int a=0, b=10, c=50, d=100; a<b && c>d; a++, c++, d–)
{
// do Stuff
}
// Not Valid cannot have commas in condition part of loop only boolean logic
for(int q=0, b=10, c=50, d=100; a<b, c>d ; a++, c++, d–)
{
// do Stuff
}
//valid
int a=10;
for(; a<10; a++)
{
// do Stuff
}

// not valid cannot initialse b in condition part of for loop
for(;int b<4; b++)
{
// do Stuff
}