/* Craig Persiko
FinalReview2.java
Review program for final exam written in class 12/14
CS 111A
*/
import java.util.Scanner;
public class FinalReview2
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int i = 6;
System.out.println("fun(i) = " + fun(i));
String instructorName = "Craig", s;
System.out.print("Enter your name ");
s = scan.next();
if (s.equals(instructorName))
System.out.println("You have the same name as the instructor");
else
System.out.println("Hello");
double[] nums;
int indexOfMax, size;
nums = getArray();
size = nums.length;
indexOfMax = findIndexOfMax(nums);
System.out.println("The highest number is: " + nums[indexOfMax] + ", last found at location: " + indexOfMax);
size = removeEvens(nums, nums.length);
System.out.println("New length is: " + size);
System.out.println("Here is the full array: ");
for(i=0; i<size; i++)
System.out.print(nums[i] + " ");
System.out.println();
}
static int fun(int x)
{
System.out.println("inside fun: x=" + x + " bar(x) = " + bar(x, x+3));
return x-1;
}
static int bar(int a, int b)
{
return a*b;
}
public static int findIndexOfMax(double[] a)
{
double max = Double.NEGATIVE_INFINITY;
int indexOfMax = -1;
for(int i=a.length-1; i>=0; i--)
{
if(a[i] > max)
{
max = a[i];
indexOfMax = i;
}
}
return indexOfMax;
}
public static double[] getArray()
{
int size;
double[] nums;
System.out.print("How many numbers will you enter? ");
size = scan.nextInt();
nums = new double[size];
System.out.println("Please enter your numbers");
for(int i=0; i<size; i++)
nums[i] = scan.nextDouble();
return nums;
}
public static int removeEvens(double[] a, int size)
{
for(int i=0; i<size; )
if(a[i]%2 == 0)
{
for(int j=i; j<size-1; j++)
a[j] = a[j+1];
size--;
}
else
i++;
return size;
}
}
/* Sample Output:
[cpersiko@fog cs111a]$ javac FinalReview2.java
[cpersiko@fog cs111a]$ java FinalReview2
inside fun: x=6 bar(x) = 54
fun(i) = 5
Enter your name f
Hello
How many numbers will you enter? 5
Please enter your numbers
1 2 4 6 3
The highest number is: 6.0, last found at location: 3
New length is: 2
Here is the full array:
1.0 3.0
[cpersiko@fog cs111a]$ java FinalReview2
inside fun: x=6 bar(x) = 54
fun(i) = 5
Enter your name Craig
You have the same name as the instructor
How many numbers will you enter? 7
Please enter your numbers
1 5 2 5 3 5 4
The highest number is: 5.0, last found at location: 5
New length is: 5
Here is the full array:
1.0 5.0 5.0 3.0 5.0
[cpersiko@fog cs111a]$ java FinalReview2
inside fun: x=6 bar(x) = 54
fun(i) = 5
Enter your name craig
Hello
How many numbers will you enter? 5
Please enter your numbers
2 4 5 6 7
The highest number is: 7.0, last found at location: 4
New length is: 2
Here is the full array:
5.0 7.0
[cpersiko@fog cs111a]$
*/
syntax highlighted by Code2HTML, v. 0.9