In: Computer Science
String entry = {{"Florence", "735-1234", "Manila"},{"Joyce", "983-3333", "Quezon City"},{"Becca", "456-3322", ,Manila"}};
Print the following entries on screen in the following format:
Name : Florence
Tel. # : 735-1234
Address : Manila
1)
Code:
import java.util.*;
class SGF
{
int a;
// calculates Sum of all the prices
void sum(int arr[], int n)
{
int s = 0;
for (int i = 0; i < n; i++)
s = s + arr[i];
System.out.println("Sum of all the prices is: "+s);
}
// Calculates Prices Less than
5.
void less(int arr[], int n)
{
int c=0;
int brr[] = new int[20];
for (int i = 0; i < n; i++)
if(arr[i]<5)
{
brr[c] = arr[i];
c++;
}
System.out.println("Prices Less than 5 are: ");
for (int i = 0; i < c; i++)
{
System.out.println(brr[i]+"\t");
}
}
// Calculates Average Value of all the
prices
void avg(int arr[], int n)
{
int s = 0;
for (int i = 0; i < n; i++)
s = s + arr[i];
a = s/n;
System.out.println("Average Value of all the prices is: "+a);
}
// Calculates Prices Higher than Average
void high(int arr[], int n)
{
int c=0;
int brr[] = new int[20];
for (int i = 0; i < n; i++)
if(arr[i]>a)
{
brr[c] = arr[i];
c++;
}
System.out.println("Prices Higher than Average are: ");
for (int i = 0; i < c; i++)
{
System.out.println(brr[i]+"\t");
}
}
// Prints the array
void printArray(int arr[], int n)
{
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
SGF ob = new SGF();
int arr[] = new int[20];
int n;
System.out.println("Enter Size of Array:");
n = sc.nextInt();
System.out.println("Enter Prices into Array:");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
ob.printArray(arr,n);
ob.sum(arr,n);
ob.less(arr,n);
ob.avg(arr,n);
ob.high(arr,n);
}
}
Note: please refer screen shots for further clarification and we can use float, int etc.. values. In this program I use int values to calculate sum,avg,less than etc...
2)
Sorting:
Code:
import java.util.*;
class BubbleSort
{
// Sorts In Ascending Order
void ascbubbleSort(int arr[], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
// Sorts In Descending Order
void descbubbleSort(int arr[], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] < arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
// Prints Array
void printArray(int arr[], int n)
{
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
BubbleSort ob = new BubbleSort();
int arr[] = new int[10];
int n;
System.out.println("Enter Size of Array:");
n = sc.nextInt();
System.out.println("Enter Elements into Array:");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
ob.ascbubbleSort(arr,n);
System.out.println("Sorted array in Ascending Order:");
ob.printArray(arr,n);
ob.descbubbleSort(arr,n);
System.out.println("Sorted array in Descending Order:");
ob.printArray(arr,n);
}
}
3)
Matrix Problem
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[][]=new int[2][3];
int b[][]=new int[2][3];
System.out.println("Enter Elements for 2 * 3 matrix:");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
//Copies Elements from One Matrix To Another
matrix
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=a[i][j];
}
}
//creating another matrix to store the sum of two
matrices
int c[][]=new int[2][3]; //2 rows and 3 columns
//adding and printing addition of 2 matrices
System.out.println("Addition of Two matrices are:");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}
}