Assignment 2
Assignment 2
Program 1
Write a program which creates an integer array and displays sum of its elements.
import java.util.Scanner;
public class class7 {
Program 2
Write a program which performs addition of elements which are stored in two arrays of type
double.
Arrays lengths may be variable in size. The resultant values must be stored in an integer array.
Display the resultant integer array in a formatted way.
import java.util.*;
Output:
Write a method that receives a name as parameter and prints on the console. “Hello, <name>!”
import java.util.Scanner;
public class class9 {
}
public static void hello(String s) {
System.out.println("Hello,"+s+"!");
}
}
Program 4
Create a method GetMax(int a, int b, int c), that returns maximal of three numbers. Write a
program that reads three numbers from the console and prints the biggest of them.
import java.util.Scanner;
public class class10 {
Program 5
Write a method that prints the digits of a given decimal number in a reversed order.
import java.util.Scanner;
public class class11{
static void rev(int n) {
int r,sum=0;
int temp=n;
while(n>0) {
r=n%10;
sum=sum*10+r;
n=n/10;
}
System.out.println("Reverse order is "+sum);
}
Program 6
Write a Boolean method IsPrime(n) that check whether a given integer number n is prime.
import java.util.Scanner;
}
}
Program 7
Write a method that calculates all prime numbers in given range and returns them as list of
integers
Write a method to print a list of integers. Write a program that takes two integer numbers (each
at a separate line) and prints all primes in their range, separated by a comma.
import java.util.ArrayList;
import java.util.Scanner;
Program 8
Write a program that can calculate the area of four different geometry figures - triangle, square,
rectangle and circle.
import java.util.Scanner;
public class class14 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter figure type");
System.out.println("1.triangle 2.Squre 3.Rectangle 4.circle");
int n=sc.nextInt();
switch (n){
case 1:
System.out.println("enter side and height of triangle");
int s=sc.nextInt();
int h=sc.nextInt();
System.out.println("Area of triangle is "+triangle(s,h));
break;
case 2:
System.out.println("enter side of square");
int l=sc.nextInt();
System.out.println("Area of square is "+square(l));
break;
case 3:
System.out.println("enter length and breadth rectangle");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Area of Rectangle is "+Rectangle(a,b));
break;
case 4:
System.out.println("enter radius of circle");
double r=sc.nextInt();
System.out.println("Area of circle is "+circle(r));
break;
default:
System.out.println("Invalid option");
}
}
private static double circle(double r) {
return 3.44*r*r;
}
private static double Rectangle(int a, int b) {
return a*b;
}
private static double square(int l) {
return l*l;
}
Program 9
Write a method which accepts two integer arrays and returns an array of unique elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class class15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int arr1[]= {10,5,20,15,25,30};
int arr2[]= {50,12,5,30,15,70};
System.out.println("Elements in Array1 is: "+Arrays.toString(arr1));
System.out.println("Elements in Array2 is: "+Arrays.toString(arr2));
uniqElements(arr1,arr2);
sc.close();
}
private static void uniqElements(int[] arr1, int[] arr2){
boolean contains = false;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr1[i]);
}
else{
contains = false;
}
}
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr2[i]);
}
else{
contains = false;
}
}
System.out.println(list);
}
}
Program 10
Write a method which accepts two matrices of Size N X N and returns summation of resultant
Matrix.
package lesson1;
import java.util.Scanner;
Write a method public static boolean isRowMagic(int[][] a) that checks if the array is row-magic
(this means that every row has the same row sum).*/
package lesson1;
import java.util.Scanner;
Write a method public static boolean isMagic(int[][] a) that checks if the array is a magic square.
import java.util.Arrays;
public class class18
{
public static void main(String []args)
{
int[][] a = {{4,9,2}, {3,5,7}, {8,1,6}};
final int n=a.length;
final int nSquare=n*n;
final int targetSum=n*(n*n+1)/2;
boolean[] flag= new boolean[n*n];
int row, col, sum;
System.out.println("Original Matrix is:");
for (int i = 0;i< a.length;i++)
{
System.out.println(Arrays.toString(a[i]));
}
for(row=0; row< n; row++)
{
sum=0;
System.out.print("row "+row+": ");
for (col=0; col< n; col++)
{
int value = a[row][col];
sum += value;
if (col > 0)
System.out.print(" + ");
System.out.print(value);
}
System.out.println(" = "+sum);
if (sum != targetSum)
{
System.out.println("Row sum incorrect : Not a magic Square:");
return;
}
}
sum=0;
System.out.print("diagonal: ");
for (int pos=0; pos< n; pos++)
{
row = n-1 - pos;
col = pos;
int value = a[row][col];
sum += value;
if ( pos > 0 )
System.out.print(" + ");
System.out.print(value);
}
System.out.println(" = "+sum);
if ( sum != targetSum )
{
System.out.println("Diagonal is incorrect : Not a magic Square:");
return;
}
for ( row=0; row< n; row++ )
{
for ( col=0; col< n; col++ )
{
int num = a [ row ][ col ];
if ( n < 1 || num > nSquare )
{
System.out.println("Number out of range : Not a magic Square:");
return;
}
if ( flag [ num-1 ])
{
System.out.println("Duplicate number : Not a magic Square :");
return;
}
flag[num-1] = true;
}
}
System.out.println("The Given Matrix is MagicSquare ");
}
}