JAVA [Q2] We need to practice more on static methods that handle arrays . Write
ID: 3775726 • Letter: J
Question
JAVA
[Q2]
We need to practice more on static methods that handle arrays
. Write a project called
ArraysStaticMethodsDemo
. It has
the following
two
classes:
Part1
(Implementation class)
Arrays
StaticMethods
•
which
has
four
static
methods:
1.
readInputs:
it reads the array size from the user, and then it reads a corresponding entries
to
that size. E.g., if a
user enters 10 for the array size, then we declare an array of 10 and then read 10 entries
and
store them in the array
. (Remember
the
Run
-time Array
Length
!).
2.
swap
: it finds the
position
(or
index
) of the maximum and minimum values in the arra
y, and
swap
them (move the biggest element to the position of the smallest, and move the smallest element to the position of
the biggest).
3.
even
: Display "true" if there is an even number of even numbers among these 10. Otherwise,
display
"false".
4.
displayOutputs:
it displays the array elements
.
Part2
: (Testing class)
Arrays
Static
MethodsDemo
•
Make sure you display clear out put messages to the user.
•
Since we are calling static methods, no need to create an object of class
Arrays
Static
Methods
What length is the array?
4
Enter an integer:
11
Enter an integer:
22
Enter an integer:
33
Enter an integer:
44
The array before: [11, 22, 33, 44]
Does the
array contain an even number of evens? true
The array after: [44, 22, 33, 11]
(
Temperature.java
) Write a program that reads in an integer N, and randomly generates N temperature
values for the past N days. Each temperature should be an integer in the range 60
-80. It should then display the average
of
temperatures over the N days and how many days
were above-
average in temperature.
•
Hint: need two loops, one to calculate the average and the second one to count how many days above such an average.
•
Sample run:
Enter a number:
7
I will find the average of 7 random degrees in the range 60-
80
Random degrees: [71.0, 80.0, 71.0, 78.0, 63.0, 75.0, 66.0]
Average temperature: 72.0
Degree(s) above average: 3
Explanation / Answer
Part 1 :
public class Implementation
{
static int size;
static int[size] array=new int[size];
static int[size] new_array=new int[size];
public static void readInputs()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter length of array ");
size=sc.nextInt();
for(int i=0;i<size;i++)
{
System.out.println("Enter Integer"):
array[i]=sc.nextInt();
new_array[i]=sc.nextInt();
}
}
public static void swap(int[] array)
{
static int temp;
Arrays.sort(array);
Static int min=array[0];
Static int max=array[array.length-1];
temp=min;
min=max;
max=temp;
new_array[0]=min;
new_array[array.length-1]=max;
}
public static void even(int[] array)
{
for(int i=0;i<array.lenth;i++)
{
static count=0;
if(array[i]%2==0)
{
count++;
}
}
if(count%2==0)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
public static void displayOutputs(int[] array)
{
System.out.println("The array before:");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
System.out.println("Does the array contains even number of evens?");
Implementation.even();
System.out.println("The arry after :");
for(int i=0;i<array.length;i++)
{
System.out.println(new_array[i]);
}
}
}
class IMP
{
public static void main (String args[])
{
Implementation.dislayOutputs();
}
}
Part 2 :
import java.util.Random;
public class Temparature
{
public static void main(String args[])
{
int sum=0,count=0;
float min=60.0;
float max=80.0;
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[size] temp=new int[size];
for (int i=0;i<size;i++)
{
temp[i]=rand.nextInt((max-min)+1)+min;
System.out.println(temp[i]);
}
for (int i=0;i<size;i++)
{
sum=sum+temp[i];
}
int avg=sum/size;
System.out.println("Average temprature:"+avg);
for(int i=0;i<size;i++)
{
if(temp[i]>avg)
{
count++;
}
}
System.out.println("Degree(s) above average:"+count);
}
}
Thank you for asking CHEGG.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.