Week 11 rogi Exam 2 3 Methods ill in the following Example ts are provided in th
ID: 3701704 • Letter: W
Question
Week 11 rogi Exam 2 3 Methods ill in the following Example ts are provided in the provided so they run according to their specifications 6. (6 points) evensTozero: Given an array of integers, change all the even integers in the array to 0. I (1,2,3,4,5 >(1,0,3,0,5 II (74,3,4,2,74,75,2} (,3,e,0,0,75,0 public static void evensToZero(int[] arr) t 7. (10 points) sumofTens: Given a two dimensional array of integers, return the sum of all the integers in the array that are multiples of 10. // {(10,5,4.10), ' {74, 75,80)}-.-10? /I ti-10, 50, 17)) r) ( public static int sumof(in
Explanation / Answer
6 and 7th Methods Implements:
// Declare Main class TestMethods
class TestMethods
{
// Declare and implement evensToZero() method
public static void evensToZero(int [] arr)
{
int [] a=new int[arr.length]; // create 1D array "a"
for(int i=0;i<arr.length;i++) // create for loop until length of an array
{
if(arr[i]%2==0) a[i]=0; //check condition for even number then assign 0 else as usual
else
a[i]=arr[i];
System.out.print(a[i]+" "); // display only odd numbers
}
}
// Declare and implement sumOfTens() method
public static int sumOfTens(int [][] arr)
{
int sum=0; // delcare and initialize sum=0
for(int i=0;i<arr.length;i++){ // create for loop until row length
for(int j=0;j<arr[0].length;j++){ // create for loop until col length
if(arr[i][j]%10==0) // check condition for multiple of 10's
sum+=arr[i][j]; // then sum of multiples of 10
System.out.print(arr[i][j]+" "); // display matrix elements
}
System.out.println();
}
return sum; // return sum of 10 multiples
}
// main method
public static void main(String args[])
{
// declare constant variables
int a[]={1,2,3,4,5};
int a1[]={74,3,4,2,74,75,2};
int a2[]={1,3,5};
int b[][]={{1,2,3},{3,4,5},{20,0,0}};
int c[][]={{10,5,4,10},{74,75,80,0}};
int d[][]={{-10,50,17}};
evensToZero(a); // calling evensToZero() method
System.out.println();
evensToZero(a1); //calling evensToZero() method
System.out.println();
evensToZero(a2); //calling evensToZero() method
System.out.println();
System.out.println(" Matrix ");
int s1=sumOfTens(b); // calling sumOfTens() method and receive multiples of 10
System.out.println("Sum of Integer in the array that are multiples of 10: "+s1); // display sum
System.out.println();
int s=sumOfTens(c); // calling sumOfTens() method and receive multiples of 10
System.out.println("Sum of Integer in the array that are multiples of 10: "+s); // display sum
int s2=sumOfTens(d); // calling sumOfTens() method and receive multiples of 10
System.out.println("Sum of Integer in the array that are multiples of 10: "+s2); // display sum
}
}
OUTPUT:
F:>javac TestMethods.java
F:>java TestMethods
1 0 3 0 5
0 3 0 0 0 75 0
1 3 5
Matrix
1 2 3
3 4 5
20 0 0
Sum of Integer in the array that are multiples of 10: 20
10 5 4 10
74 75 80 0
Sum of Integer in the array that are multiples of 10: 100
-10 50 17
Sum of Integer in the array that are multiples of 10: 40
FILE READING PROGRAM
/* Text File book.txt contains
Welcome to Java
Java Programming Language
Java is Object Oriented Programming Language
This is HIgh Level Programming Language */
import java.io.*;
// Create main Class PrintText
class PrintText
{
public static void main(String [] args)
{
BufferedReader reader; // Declare reader Object for BufferedReader
// using exception block
try{
reader=new BufferedReader(new FileReader("F:\book.txt")); // open file in read mode
String str=reader.readLine(); // read each line in the text file
while(str!=null) // create while loop until end of file
{
System.out.println(str); // display each line in the file
str=reader.readLine(); // read text file
}
reader.close(); // close file
}catch(IOException e)
{
System.out.println(e); // catch exception if file is not available
}
}
}
OUTPUT
F:>javac PrintText.java
F:>java PrintText
Welcome to Java
Java Programming Language
Java is Object Oriented Programming Language
This is HIgh Level Programming Language
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.