Skip to content
Open

ss #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Main.class
Binary file not shown.
6 changes: 6 additions & 0 deletions Main.ctxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#BlueJ class context
comment0.target=Main
comment1.params=args
comment1.target=void\ main(java.lang.String[])
comment1.text=\r\n\ This\ one\ will\ have\ public\ static\ void\ main\r\n
numComments=2
155 changes: 152 additions & 3 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,35 @@ public static void main( String[] args)
* call this array ar1. Print it out below.
*/
System.out.println("*** PRINTING ar1 *");

int[] ar1;
ar1 = new int [n];
for (int i=0; i<ar1.length; i++)
ar1[i]=i;
for (int i=0; i<ar1.length; i++)
System.out.println("ar1["+i+"] = "+ar1[i] );

/*
* Task 2. Create a new array called ar2.
* Copy the elements of ar1 into ar2.
*/

int[] ar2;
ar2 = new int [n];
for (int i=0; i<ar2.length; i++)
ar2[i]=i;
for (int i=0; i<ar2.length; i++)
System.out.println("ar2["+i+"] = "+ar2[i] );

/*
* Task 3. Add 1 to each element in ar1. Print it out below
*/
System.out.println("** Stask 3 **");
int value;
for (int i=0 ; i<ar1.length ; i++)
{
ar1[i] = ar1[i] +1;
}
for (int i=0 ; i<ar1.length ; i++)
System.out.println("ar1[" + i + "] = "+ar1[i]);



Expand All @@ -52,12 +70,44 @@ public static void main( String[] args)
* ar1: 1 2 3
* ar3: 1 2 3 0 1 2 3
*/
System.out.println("** test 4 **");
int[] ar3 = new int[ ar1.length *2 ];
for (int i=0 ; i< ar1.length ; i++)
{
ar3[i]=ar1[i];
}

for (int i=0 ; i<ar1.length ; i++)
{
ar3[i+ar1.length]=ar1[i];
}

for (int i=0 ; i<ar3.length ; i++)
System.out.println("ar3[" + i + "] = "+ar3[i]);

/*
* Task 5. Switch the first and last element of ar1.
* print out the new ar1. Then switch them back
*/
System.out.println("** Task 5**");

// Ye olde switcheroo

value = ar1[0]; // backup ar1[0] into a bitbucket

ar1[0] = ar1[ar1.length-1]; // copy last into ar1[0].

ar1[ar1.length-1]=value; // copy old value of ar1[0] into last


// Print out the array
for (int i=0 ; i<ar1.length ; i++)
System.out.println("ar1[" + i + "] = "+ar1[i]);

// The Restoration of the rightful heir
value = ar1[0];
ar1[0] = ar1[ar1.length-1];
ar1[ar1.length-1] = value;

/*
* Task 6A. Print the 2nd to (n-1)th elements of ar1
Expand All @@ -66,6 +116,28 @@ public static void main( String[] args)
* the indices are multiples of 3
*
*/
System.out.println(" ** Task 6A **");

// print 2nd to (n-1)th element
for (int i=1 ; i<ar1.length-1 ; i++)
System.out.println(ar1[i]);


System.out.println(" ** Task 6B **");
// print odd numbers in ar1
// AKA if ar1[i] is odd, print it out.
for (int i=0 ; i<ar1.length ; i++)
if (ar1[i]%2==1)
System.out.println(ar1[i]);


System.out.println(" ** Task 6C **");
// print elements if it is a multiple of 3
// AKA if ar1[i] is divisble by 3, print it out.
// AKA if ar1[2] has a zero as the remainer when divided by 3
for (int i=0 ; i<ar1.length ; i++)
if (ar1[i]%3==0)
System.out.println(ar1[i]);

/*
* Task 7. For each element in ar1,
Expand All @@ -78,6 +150,16 @@ public static void main( String[] args)
* ar[2]=30
* ar[3]=4
*/
System.out.println("** Task 7 **");

for (int i=0 ; i<ar1.length ; i++)
{
if ( ar1[i]%2==1)
ar1[i] *=10;
}

for (int i=0 ; i<ar1.length ; i++)
System.out.println(ar1[i]);

/*
* Task 8
Expand All @@ -89,25 +171,92 @@ public static void main( String[] args)
* ar2[2]=2
* ar2[3]=3 -> ar2odds[1]=3
*/
System.out.println(" ** Task 8 **");
int k=0;
int[] ar2odds = new int[ar2.length]; // too big right now.
for ( int i=0 ; i<ar2.length ; i++)
{
System.out.println(ar2[i]); // Just print it out to see

if (i%2==1)
{
ar2odds[k]=ar2[i];
k++;
}
}

for (int i=0 ; i<k ; i++)
System.out.println(ar2odds[i]);

/*
* Task 9. In the array ar2, count how many odd numbers you
* have. Then create an ew array called ar4. Copy just the odd
* numbers from ar1 into ar4. Print ar4
*/
int l4=0;
for (int i=0 ; i<ar2.length ; i++)
{
if ( ar2[i]%2==1)
l4++;
}
int[] ar4 = new int[l4];
k=0;
for (int i=0 ; i<ar2.length ; i++)
{
if (ar2[i]%2==1)
{
ar4[k]=ar2[i];
k++;
}
}

for (int i=0 ; i<k ; i++)
System.out.println(ar4[i]);

/*
* Task 10. Shift the elements of ar4 right by 1
* For example
* old ar4: 1 3 5 7 9
* new ar4 9 1 3 5 7
*/
System.out.println("** Task 10 ** ");
System.out.println("Original ar4");
for (int i=0 ; i<ar4.length ; i++)
System.out.println(ar4[i]);


int temp10=ar4[ar4.length-1];

for (int i=ar4.length-1 ; i>0 ; i--)
ar4[i]=ar4[i-1];

ar4[0]=temp10;

System.out.println("New ar4");
for (int i=0 ; i<ar4.length ; i++);
System.out.println(ar4[0]);


/*
* Task 11. Reverse the order of elements in ar2
*/

System.out.println("** Task 11 **");
for (int i=0 ; i<ar2.length ; i++)
System.out.println("ar2["+i+"] = "+ar2[i]);

int ar2half = ar2.length/2;
System.out.println(ar2half);
int task11Temp;
for (int i=0 ; i<ar2half ; i++)
{
task11Temp = ar2[i];
System.out.println(" "+i+":"+(ar2.length-i));
ar2[i] = ar2[ar2.length-1-i];
ar2[ar2.length-1-i]=task11Temp;
}

for (int i=0 ; i<ar2.length ; i++)
System.out.println("ar2["+i+"] = "+ar2[i]);

/*
* Task 12:
Expand Down
Binary file added nidd'/Main.class
Binary file not shown.
6 changes: 6 additions & 0 deletions nidd'/Main.ctxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#BlueJ class context
comment0.target=Main
comment1.params=args
comment1.target=void\ main(java.lang.String[])
comment1.text=\r\n\ This\ one\ will\ have\ public\ static\ void\ main\r\n
numComments=2
Loading