1.Define a class Array with following member Field: int data[]; Function: Array( ) Array(int size) Array(int data[])void Reverse _an _array ()int Maximum _of _array () int Average_of _array()void Sorting () void display() int search(int no) int size();
Use all the function in main method. Create different objects with different constructors.
import java.util.Scanner;public class Array {private int data[];private int sizee;Array() {sizee = 10;data = new int[10];}Array(int size) {sizee = size;data = new int[size];}Array(int size[]) {data = size;}void reverseanarray() {int i;for (i = sizee - 1; i >= 0; i--) {System.out.print(data[i]);}}void insert() {int i;Scanner sc = new Scanner(System.in);for (i = 0; i < sizee; i++) {data[i] = sc.nextInt();}}int maximum_of_array() {int temp, i;temp = data[0];for (i = 0; i < sizee; i++) {if (data[i] > temp) {temp = data[i];}}return temp;}float averageofarray() {int i, sum = 0;for (i = 0; i < sizee; i++) {sum = sum + data[i];}return (sum / sizee);}int finalfunc(int beg, int finl){int pivot = data[finl], swap;int i = 0;int temp = beg - 1;for (i = beg; i <= finl; i++){if (pivot > data[i]) {temp++;swap = data[temp];data[temp] = data[i];data[i] = swap;}}temp++;swap = data[temp];data[temp] = data[finl];data[finl] = swap;return temp;}void quicksort(int beg, int finl) {int partition;if (beg < finl) {partition = finalfunc(beg, finl);System.out.print(partition);quicksort(beg, partition- 1);quicksort(partition + 1, finl);}}void sorting() {int beg = 0, finl = sizee - 1;quicksort(beg, finl);}void display (){int i;for(i=0;i<sizee;i++){System.out.print(data[i]);}}int search (int val){int i,k=-1;for(i=0;i<sizee;i++){if(data[i]==val){k = i;}}return k;}int size(){return sizee;}}public class Main {public static void main(String[] args) {int max0foj,size;float avegofarray;Array a1=new Array();Array a2=new Array((4));a1.insert();a2.insert();a1.reverseanarray();a2.reverseanarray();max0foj=a1.maximum_of_array();System.out.println(max0foj);avegofarray=a1.averageofarray();System.out.println(avegofarray);a1.sorting();a1.display();a1.search(5);size=a1.size();}}
2-(2)
Define a class Matrix with following Field:
int row, column; float mat[][] Function: Matrix(int a[][]) Matrix() Matrix(int rwo, int col) void readMatrix() float [][] transpose( )
float [][] matrixMultiplication(Matrix second ) void displayMatrix(float [][]a) void displayMatrix() float maximum_of_array() float average_of_array( ) create three object of Matrix class with different constructors in main and test all the functions in main…
package com.company;import java.util.Scanner;public class matrix {static int [][] mat1=new int [3][3] ;static int [][] mat2=new int[3][3];static int [][] trans=new int [3][3];static int [][] mul=new int [3][3];static Scanner sc=new Scanner (System.in);static void insert (int s){if(s==1){int i,j;for(i=0;i<3;i++){for(j=0;j<3;j++){mat1[i][j]=sc.nextInt();}}}else if(s==2){int i,j;for(i=0;i<3;i++){for(j=0;j<3;j++){mat2[i][j]=sc.nextInt();}}}}static void transpose (int s){int i,j;if(s==1){for(i=0;i<3;i++){for(j=0;j<3;j++){trans[i][j]=mat1[j][i];}}}else if(s==2){for(i=0;i<3;i++){for(j=0;j<3;j++){trans[i][j]=mat2[j][i];}}}}static void multiplication (){int i,j,k;for(i=0;i<3;i++){for(j=0;j<3;j++){mul[i][j]=0;for(k=0;k<3;k++){mul[i][j]+=mat1[i][k]*mat2[k][j];}}}}static void print (int v){int i,j;if(v==1){for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.print(" "+trans[i][j]);}System.out.println(" ");}}if(v==2){for(i=0;i<3;i++){for(j=0;j<3;j++){System.out.print(" "+mul[i][j]);}System.out.println(" ");}}}}public class Main {public static void main(String[] args) {matrix.insert(1);matrix.insert(2);matrix.multiplication();matrix.transpose(1);matrix.print(1);matrix.transpose(2);matrix.print(1);matrix.print(2);}}
2-(3)
Write a program to demonstrate usage of different methods of Wrapper class
package com.company;import java.util.*;public class Main{public static void main(String[] args) {Scanner sc=new Scanner(System.in);int i=sc.nextInt();String s= sc.nextLine();Integer obint= i;System.out.println(obint);System.out.println(s);System.out.println("enter size of array");int n=sc.nextInt();Integer[] arr=new Integer[n];Vector <Integer> v= new Vector<>();Collections.addAll(v, arr);System.out.println("Vector contains: " + v);}}
2-(4,5)
Write a program to demonstrate usage of String and StringBuffer class
Define a class Cipher with following data Field: String plainText; int key Functions: Cipher(String plaintext,int key) String Encryption( ) String Decryption( ) Read string and key from command prompt and replace every character of string with character which is key place down from current character. Example plainText = “GCET” Key = 3 Encryption function written following String “ JFHW” Decryption function will convert encrypted string to original form “GCET”
package com.company;public class encryptiondecryption{static String basic,encrypted,decrypted;static int key;encryptiondecryption (String v,int k){basic=v;key=k;}static StringBuffer Encryptedstring(){StringBuffer s=new StringBuffer();int i,j;for(i=0;i<basic.length();i++){char c=basic.charAt(i);j=(int)c;j=j+key;c=(char)j;s.append(c);}encrypted= String.valueOf(s);return s;}static StringBuffer decryptedstring (){StringBuffer s=new StringBuffer();int i,j;for(i=0;i<encrypted.length();i++){char c=encrypted.charAt(i);j=(int)c;j=j-key;c=(char)j;s.append(c);}decrypted= String.valueOf(s);return s;}}public class Main {public static void main(String[] args) {encryptiondecryption v=new encryptiondecryption("vedant",2);System.out.println(v.Encryptedstring());System.out.print(v.decryptedstring());}}
0 Comments