Week 3: Import Java - Util.scanner Public Class Sum - Avg - Array (Public Static Void Main (String Args ) (
Week 3: Import Java - Util.scanner Public Class Sum - Avg - Array (Public Static Void Main (String Args ) (
import java.util.Scanner;
for(int i=0;i<n;i++)H
{
sum+=arr[i];
}
avg=sum/n;
System.out.println("Sum is "+sum);
System.out.println("Avg is "+avg);
}
}
Enter 10 values
1
2
3
4
5
6
7
8
9
10
Sum is 55
Avg is 5
import java.util.Scanner;
public class Stack {
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
}
10 pushed into stack
Peek of the stack 10
20 pushed into stack
Peek of the stack 20
30 pushed into stack
Peek of the stack 30
30 Popped from stack
int dequeue()
{
if (isEmpty(this))
return Integer.MIN_VALUE;
int front()
{
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.front];
}
int rear()
{
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.rear];
}
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
System.out.println(queue.dequeue() +
" dequeued from queue\n");
Front item is 20
Rear item is 40
import java.util.Scanner;
import java.util.Arrays;
424
6. Write a Java program to search an element in an array.
int n = arr.length,result=-1;
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
result=i;
}
if(result == -1)
System.out.print("Element is not present in array");
else
System.out.print("Element is present at index " + result);
}
}
Element is present at index 3
7. Write a Java program to find the sum of even numbers in an integer array.
import java.util.Scanner;
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
sum+=arr[i];
}
System.out.println("Sum of even ="+sum);
}
}
Enter 10 values
1
2
3
4
5
6
7
8
9
10
Sum of even =30
}
static public void main (String[] args)
{
int [][]a = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 } };
printDiagonalSums(a, 4);
}
}
Diagonal: 18
10. Write a Java program to enter n elements in an array and find smallest number among
them.
public class Reverse_An_Array {
if(min>arr[k])
min=arr[k];
}
11. Write Java program to find the sum of all odd numbers in a 2D array.
printDiagonalSums(a, 4);
}
}
Sum of odd: 32
13. Write a Java program to check whether a given matrix is sparse or not.
import java.util.Scanner;
sc.close();
}
}
Enter the dimensions of the matrix:
2
2
Enter the elements of the matrix:
0
0
0
1
The matrix is a sparse matrix
for(int k=0;k<9;k++){
int i,m=0,flag=0;
int n=arr[k];
m=n/2;
if(n==0||n==1){
}else{
for(i=2;i<=m;i++){
if(n%i==0){
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}
}
}}
2 is prime number
3 is prime number
5 is prime number
7 is prime number
int first,second;
first = second = arr[0];
for (int i = 0; i < 9; i++)
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
}}
2nd is : 8
16. Write a Java program which counts the non-zero elements in an integer array.
public class NonZero_Count {
int count=0;
for (int i = 0; i < 9; i++)
{
if(arr[i]!=0)
count++;
}
System.out.println("Non zero element : "+count);
}}
Non zero element : 5
import java.util.Scanner;
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[c][d] = sum;
sum = 0;
}
}
System.out.print("\n");
}
} }
}
Enter the number of rows and columns of first matrix
2
2
Enter elements of first matrix
10
20
30
40
Enter the number of rows and columns of second matrix
2
2
Enter elements of second matrix
40
30
20
10
Product of the matrices:
800 500
2000 1300
20. Write a Java program to subtract two matrices.
import java.util.Scanner;
21. Write a Java program to find duplicate elements in a 1D array and find their frequency of
occurrence.
import java.util.Scanner;
int j = 0;
for (int i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];
for (int i=0; i<j; i++)
arr[i] = temp[i];
for (int i=0; i<j; i++)
System.out.print(arr[i]+" ");
}
}
12345
22. Write a Java program to print every alternate number of a given array.
import java.util.Scanner;
public Constructor_Zero_Arg() {
System.out.println("Hello");
}
}
Hello
}
Hello
public Constructor_OverLoad() {
System.out.println("UEMJ");
}
String str="UEMK";
Constructor_OverLoad obj=new Constructor_OverLoad(str);
}
UEMK
27. Write a class, Grader, which has an instance variable, score, an appropriate constructor and
appropriate methods. A method, letterGrade() that returns the letter grade as O/E/A/B/C/F.
Now write a demo class to test the Grader class by reading a score from the user, using it to
create a Grader object after validating that the value is not negative and is not greater then 100.
Finally, call the letterGrade() method to get and print the grade.
import java.util.Scanner;
float avg;
Scanner scanner = new Scanner(System.in);
}
Enter the %: 91
The student Grade is: A
28. Write a class, Commission, which has an instance variable, sales; an appropriate
constructor; and a method, commission() that returns the commission.
Now write a demo class to test the Commission class by reading a sale from the user, using it to
create a Commission object after validating that the value is not negative. Finally, call the
commission() method to get and print the commission. If the sales are negative, your demo
should print the message “Invalid Input”.
import java.util.Scanner;
int avg;
Scanner scanner = new Scanner(System.in);
Commission obj;
if(avg<0)
System.out.println("Invalid Input");
else
obj=new Commission(avg);
}
Enter the value: 999
Commission is 999