CS210 Programming Assignment #4 Solution

$30.00

Category:

Description

This assignment focuses on array structures and class. Be sure to include a short comment at the beginning of your program as well as a short comment for each method describing what it does. For this assignment you are limited to the language features in Chapters 1-7 shown in lecture or the textbook. You have to submit 10 java files. Do not change name of classes. (q1~q9: 2points each(no partial points), q10: 7Points)

  1. 1. Write a Java program to calculate average value of an array

public class ex1AverageValue {

public static void main(String[] args) {

int[] numbers = new int[]{10, 20, 30, 40, -50, 60, -70};

 

System.out.println(“Average value of the array elements is : ” + average);

}

}

// Average value of the array elements is : 5.0

 

  1. 2. Write a Java program to find the index of an array public class ex2FindIndex {

public static int  findIndex (int[] my_array, int t) {

 

}

public static void main(String[] args) {

int[] my_array = new int[] {25, 10, 55, 65, 36, 92, 77, 8, 13, 79}; System.out.println(“Index position of 55 is: ” + findIndex(my_array, 55)); System.out.println(“Index position of 13 is: ” + findIndex(my_array, 13));

}

}

//Index position of 55 is: 2

//Index position of 13 is: 8

 

  1. 3. Write a Java program to copy an array by iterating the import java.util.Arrays;

public class ex3CopyArray {

public static void main(String[] args) {

int[] my_array = new int[]{25, 10, 55, 65, 36, 92, 77, 8, 13, 79};

int[] new_array = new int[10];

System.out.println(“Source Array : “+Arrays.toString(my_array));

 

}

}

//Source Array : [25, 10, 55, 65, 36, 92, 77, 8, 13, 79]

//New Array: [25, 10, 55, 65, 36, 92, 77, 8, 13, 79]

 

  1. 4. Write a Java program to insert an element (specific position) into an import java.util.Arrays;

public class ex4Insert {

public static void main(String[] args) {

 

int[] my_array =new int[] {25, 10, 55, 65, 36, 92, 77, 8, 13, 79};

// Insert an element in 3rd position of the array (index->3, value->12)

int Index_position = 3;

int newValue    = 12;

System.out.println(“Original Array : “+Arrays.toString(my_array));

 

 

 

System.out.println(“New Array: “+Arrays.toString(my_array));

}

}

//Original Array : [25, 10, 55, 65, 36, 92, 77, 8, 13, 79]

//New Array: [25, 10, 55, 12, 65, 36, 92, 77, 8, 13]

 

  1. 5. Write a Java program to find the maximum and minimum value of an import java.util.Arrays;

public class ex5MinMax {

static int max;

static int min;

public static void max_min(int my_array[]) {

 

 

 

 

 

 

}

 

public static void main(String[] args) {

int[] my_array = {25, 10, 55, 65, 36, 92, 77, 8, 13, 79};

max_min(my_array);

System.out.println(” Original Array: “+Arrays.toString(my_array)); System.out.println(” Maximum value for the above array = ” + max); System.out.println(” Minimum value for the above array = ” + min);

}

}

//Original Array: [25, 10, 55, 65, 36, 92, 77, 8, 13, 79]

//Maximum value for the above array = 92

//Minimum value for the above array = 8

 

  1. 6. Write a Java program to reverse an array of integer import java.util.Arrays;

public class ex6Reverse {

 

int[] my_array1 = {2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,

2019};

System.out.println(“Original array : “+Arrays.toString(my_array1));

 

 

 

 

System.out.println(“Reverse array : “+Arrays.toString(my_array1));

}

}

//Original array : [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]

//Reverse array : [2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010]

 

 

  1. 7. Write a Java program to find the duplicate values of an array of integer values.

 

import java.util.Arrays;

public class ex7DuplicateValue {

public static void main(String[] args)

{

int[] my_array = {1, 2, 3, 3, 4, 5, 6, 2};

 

 

 

 

}

}

//Duplicate Element : 2

//Duplicate Element : 3

 

 

  1. 8. Write a Java program to find the duplicate values of an array of string values.

public class ex8DuplicateString {

public static void main(String[] args)

{

String[] my_array = {“Wilson”, “Sherman”, “Lynch”, “Chancellor”, “Graham”, “Wilson”, “Lynch”};

 

 

 

 

 

}

}

//Duplicate Element is : Wilson

//Duplicate Element is : Lynch

 

  1. 9. Write a Java program to find the TRIPLE duplicate values of an array of string

 

public class ex9TripleDuplicateString {

public static void main(String[] args)

{

String[] my_array = {“Wilson”, “Sherman”, “Lynch”, “Wilson”, “Graham”, “Wilson”, “Lynch”};

 

 

 

 

 

}

}

//Triple Duplicate Element is : Wilson

 

 

  1. 10. Complete the following java program as we discussed at the class (up- down only). You have to use the class movingObject.

 

import java.awt. * ;  import java.util. * ;

public class balls {

public static int width = 800;  public static int height = 600; public static int howMany = 20; public static int ballSizeMax = 70; public static

 

public static void main(String[] args) {

DrawingPanel panel = new DrawingPanel(width, height);

panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); getInitialStatus();

while (true) {

for (int i = 0; i < howMany; i++) {

g.setColor(

 

 

 

 

}

panel.sleep(50);

g.clearRect(0, 0, width, height);

}

}

public static void getInitialStatus() { Random rand = new Random();

for (int i = 0; i < howMany; i++) { ball[i] = new movingObject(); ball[i].size=rand.nextInt(ballSizeMax)  + 10;

 

 

 

 

 

 

 

}

}

 

public static Color getColor() {

 

 

 

 

 

return myColor;

 

}

}

}

 

int x;

int y;

int size;

int speed;

String direction; Color color;

}


error: Content is protected !!