2. Write a counter controlled loop to solve the following problems. Each one will involve an array
(MinMax.java) Read in 25 ints from the keyboard, and store them in an array. Then, find the maximum and minimum values in such an array, and display them on the screen.
public class Array-Assignment {
public static void main(String [] args) {
int [] x = new int[3];
int [] y = {3, 5, 9, 2};
x[2] = y[3];
x[0]++;
y[1] += y[2] * y[0];
int [] z = x;
x = y;
x[1] = 4;
}
}
public class Array-Length {
public static void main(String [] args) {
int [] x = new int[4];
int [] y = {};
int [] z = {0};
System.out.println("x has " + x.length + " elements");
System.out.println("y has " + y.length + " elements");
System.out.println("z has " + z.length + " elements");
}
}
public class Array-With-Loop1 {
public static void main(String [] args) {
int [] x = {-4, 9, 8, 2, -5, 7, 1};
for(int i=1; i
x[i] += x[i-1]; // notice: += instead of =
}
}
}
public class Array-With-Loop2 {
public static void main(String [] args) {
int [] x = {-4, 9, 8, 2, -5, 7, 1};
for(int i=1; i
x[i] = x[i-1];
}
}
}
public class Array-With-Loop3 {
public static void main(String [] args) {
int [] x = {-4, 9, 8, 2, -5, 7, 1};
int val = 0;
for(int i=1; i
val = val + x[i];
}
}
}
Code is after the output.
Please feel free to ask doubt/give feedback.
Output:
Answer:
import java.util.Scanner;
public class MinMax{
public static void main(String args[])
{
//create an array of size 25
int a[]=new int[25];
//declare min and max varibales
int min,max;
//create a scanner object to take input from the keyboard
Scanner sc=new Scanner(System.in);
System.out.println("Enter 25 integers");
//for loop, 25 times
for(int i=0;i<25;i++)
{
//scan a[i]
a[i]=sc.nextInt();
}
//initialize min and max to first item of the array
min=max=a[0];
//loop through the items of the array
for(int i=0;i<25;i++)
{
//use Math.min and Math.max to find min/max between previous min/max and a[i]
min=Math.min(min,a[i]);
max=Math.max(max,a[i]);
}
//print the result
System.out.println("Maximum number is "+max+"\nMinimum number is "+min);
}
}
2. Write a counter controlled loop to solve the following problems. Each one will involve an...
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...
please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...
1) Consider the following Java program: 1 public class HelloWorld { 2 // My first program! 3 public static void main(String[] args) { 4 System.out.println("Hello, World!"); 5 } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...
Java Questions When creating a for loop, which statement will correctly initialize more than one variable? a. for a=1, b=2 c. for(a=1, b=2) b. for(a=1; b=2) d. for(a = 1&& b = 2) A method employee() is returning a double value. Which of the following is the correct way of defining this method? public double employee() c. public int employee() public double employee(int t) d. public void employee() The ____ statement is useful when you need to test a...
Explain in detail what the code below does: public class MyClass { public static void main(String args[]) { System.out.println(isUniqueChars("something")); } public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = str.charAt(i) - 'a'; if ((checker & (1 << val)) > 0) return false; checker |= (1 << val); } return true;...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] hourlyTemp = new...
In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){ int total = 0; for(int x: array) total += x; return total / array.length; } public static double average(double[] array){ double total = 0; for(double x: array) total += x; return total /...
Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. public class StudentScores { public static void main (String [] args) { final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i = 0; lowerScores[0] = 5; lowerScores[1] = 0; lowerScores[2] = 2; lowerScores[3] = -3; /* Your solution goes here */...