Wednesday, October 13, 2010

HW for Oct 13, 2010: Academic Integrity

One example of cheating would be a group of students working together on an assignment/project without teacher authorization.

If you get caught cheating, you can be given a grade of XE that is noted as academic dishonesty. It goes on record in your AIP file.

Some examples of sanctions include: getting a 0 on the test or assignment, having course grade lowered by one letter grade, or in extreme and repeated cases, suspension and expulsion are also options.

The AIP file is kept in the Dean's office for 6 years or until the student graduates.

Cheating can have drastic and long lasting consequences; it is not worth the immediate gratification and success. You can even have your degree revoked for cheating.

Sunday, September 26, 2010

isSorted() algorithm

This is just a simple program that checks to see if an input list of (n) size is sorted in ascending order. It really doesn't matter how many elements are in the list because the algorithm has to check every element to determine if the list is actually sorted. There is an if statement for the last element, because you don't have to compare it to the next term in the list (as there is none, it would generate an error). Therefore, when it reaches the last element, it will return true and leave the loop.

If even one number in the list is greater than its next element, it will display that the list is indeed not sorted, and break from the loop because there is no further need to check the rest of the list.

For example, say a list of [1, 2, 4, 3] is entered. The loop would go through once, see that 1 is < 2, and then move on to 2 and 4. It would also say that 2 < 4, and then would compare the last two numbers. Because 4 is not less than 3, it would return a statement saying the list is not sorted and the program would terminate.


You could use the same numbers and put them in as [4, 1, 2, 3]. It would compare the first 2 numbers and because 4 is greater than 1, the program would return the correct statement saying the list is not sorted and the program would also terminate.
import java.util.Scanner;


public class SortCheck
{


public static void main(String[] args)
{

int i = 0;
int j = 0;
boolean isComplete = false;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers are in the list?");
int numberOfItems = scan.nextInt();
int[] list = new int[numberOfItems];
int numbersInput = numberOfItems;
//Input all the numbers into the array
while(numberOfItems > 0)
{
System.out.println("Enter a number.");
int aNumber = scan.nextInt();
list[i] = aNumber;
i++;
numberOfItems--;
}
// ASCENDING SORT CHECK
while(isComplete == false)
{

//Determines if it has reached the end of the list yet. If it has reached the end,
//it has already checked if each element is higher than its preceding entry.
if(j==numbersInput-1)
{
System.out.println("This list is sorted.");
isComplete = true;
}
else
{
//If the element is less than the next entry, it simply increments the counter
//and goes to the next one.
if(list[j] < list[j+1])
{
j++;
}
//If any element does not fulfill the condition, it will break because there is
//no need to check the rest of the numbers.
else
{
System.out.println("This list is not sorted.");
isComplete = true;
}
}
}


}

}

Sunday, September 19, 2010

Finding a number in a list

Simple program that will take a list of numbers and some test number as input. It will go through the array of numbers and compare each of them to the test number. If any of the numbers match one from the list, it will return true; otherwise it will return false.

It will terminate because the loop checking for the test number will run at least once, and at most n times (n being the number of elements in the array).

It should always return the correct answer, using logical situations to test it. For example, if the test number was 6, and the first number in the list also happened to be 6, then the loop would run once, comparing the two numbers, and set the variable to true to indicate that the test number is actually in the list, and return the related statement. If the test number happened to be the second element in the array, the loop would run twice and then set the variable to true. If the test number is not in the array, it would check every number in the list before returning false.

(Note: I included the break so that the loop would not have to continue checking the rest of the array if the test number was already found in the list before)
import java.util.Scanner;

public class FindANumber
{

public static void main(String[] args)
{
boolean inList = false;
int i = 0;
int testNumber;
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers are in the list? ");
int numberOfItems = scan.nextInt();
int[] list = new int[numberOfItems];
int numbersInput = numberOfItems;
while(numberOfItems > 0)
{
System.out.println("Enter a number into the list.");
int aNumber = scan.nextInt();
list[i] = aNumber;
i++;
numberOfItems--;
}
System.out.println("What is the number you want to test? ");
testNumber = scan.nextInt();

//Will go through each element in the array and compare to the test input. if it matches any element, it will set
//inList to be true and break from the loop so that we do not have to check the rest of the numbers.
for(int j = 0; j < numbersInput; j++)
{
if(list[j]==testNumber)
{
inList = true;
break;
}
}
if(inList == true)
{
System.out.println(testNumber + " is in the list!");
}
else
{
System.out.println(testNumber + " is NOT in the list.");
}
}

}

Sunday, September 12, 2010

Maximum Number Solution

This program will compute the max from a given set of n numbers.
This program will run a finite number of times because the first loop will run n times to input all the numbers into the array, and the second loop will run a maximum of n-1 times to check every number.

The program should return the correct answer everytime, using simple logic to test it. For example, if in a given list of 5 numbers, the maximum happened to be the first number, then max will never be reassigned to any of the other 4 numbers. If the maximum is the second number, then it will perform the check and reassign the temporary max to the second number, and that variable would never be reassigned again. If the maximum is the third number, the variable would be reassigned to the third variable during the checks, and it would never be reassigned again.


import java.util.Scanner;

public class FindMax 
{

public static void main(String[] args) 
{

int i = 0;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers are in the list that you want find the max from?");
int numberOfItems = scan.nextInt();
int[] list = new int[numberOfItems];
int numbersInput = numberOfItems;
while(numberOfItems > 0)
{
System.out.println("Enter a number.");
int aNumber = scan.nextInt();
list[i] = aNumber;
i++;
numberOfItems--;
}
int max = list[0]; 
//Sets the first number as the temporary maximum, will compare the rest of the numbers to the temporary max
for(int j = 1; j < numbersInput; j++)
{
if(list[j]>max)
{
max = list[j];
}
}
System.out.println("The maximum number is: " + max);
}

}

Tuesday, September 7, 2010

Sorting algorithim

Sorting solution for any given amount of numbers, in Java.


import java.util.Arrays;
import java.util.Scanner;
public class Sorter
{
public static void main(String[] args)
{
int i = 0;
int j = 0;
boolean isDone = false;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers do you want to sort?");
int numberOfItems = scan.nextInt();
int[] list = new int[numberOfItems];
int numbersInput = numberOfItems;
while(numberOfItems > 0)
{
System.out.println("Enter a number.");
int aNumber = scan.nextInt();
list[i] = aNumber;
i++;
numberOfItems--;
}
Arrays.sort(list);
while(isDone != true)
{
if(j == (numbersInput))
{
isDone = true;
}
else
{
System.out.println(list[j]);
j++;
}
}
}
}

Tuesday, August 31, 2010

ASU101 Assignment1

I'm Justin Lai. I was born in Canada, and lived there for 6 years before moving down to Phoenix in 1998. My family is mainly Chinese (Cantonese) and most of my relatives live in Hong Kong. I can decently speak Cantonese, but I cannot read or write in Chinese at all because I never continued my Chinese lessons when I was in Canada.

My strongest subject is probably Math, and my weakest would have to be English. I also enjoy programming and making applications. In my free time I like playing videogames to relax, or chilling with friends. I play a lot of different games, and I'm not really picky; my favorite genres would be FPSes and RPGs. I also play quite a bit of Street fighter and Starcraft.


The transition from high school to college was pretty smooth. ASU is definitely bigger than Desert Vista, but it also feels like we have more freedom, especially when it comes to the class schedule. Other than the class schedule, college life isn't that drastically different, especially because I'm living at home. College will still be a blast, I hope.