Question

In: Computer Science

Create an application containing an array that stores 20 prices, such as 2.34, 7.89,1.34, and so...

  1. Create an application containing an array that stores 20 prices, such as 2.34, 7.89,1.34, and so on. The application should (1) display the sum of all the prices, (2) display all values less than 5.00, (3) calculate the average of the prices, and (4) display all values that are higher than the calculated average value.
  2. Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
  3. Given the following multidimensional array that contains addressbook entries:

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. Write a program in Java which stores values in a 2 x 3 matrix mat1. Copy the contents to another matrix mat2, after that calculate the sum and stores the result into another matrix mat3.

Solutions

Expert Solution

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
}
   }
}



Related Solutions

Create an application containing an array that stores 5 integers. The application should call five methods...
Create an application containing an array that stores 5 integers. The application should call five methods from Array2 class that in turn (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the integers, (4) display all values less than a limiting argument, and (5) display all values that are higher than the calculated average value. Save the file as ArrayTest.java.
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then...
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then display the data. import java.time.*; public class CertOfDeposit { private String certNum; private String lastName; private double balance; private LocalDate issueDate; private LocalDate maturityDate; public CertOfDeposit(String num, String name, double bal, LocalDate issue) { } public void setCertNum(String n) { } public void setName(String name) { } public void setBalance(double bal) { } public void issueDate(LocalDate date) { } public String getCertNum() { }...
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should...
Create a python application that inputs, processes and stores student data. Specifications: 1. Your application should be able to accept Student data from the user and add the information in a file type of your choice. 2. your application is a menu driven and allow the user to choose from the following menu Menu: 1 – Add students to file 2 – print all the student information 3 – print specific student information using studentID 4 – Exit the program...
.data A: .space 80 # create integer array with 20 elements ( A[20] ) size_prompt: .asciiz...
.data A: .space 80 # create integer array with 20 elements ( A[20] ) size_prompt: .asciiz "Enter array size [between 1 and 20]: " array_prompt: .asciiz "A[" sorted_array_prompt: .asciiz "Sorted A[" close_bracket: .asciiz "] = " search_prompt: .asciiz "Enter search value: " not_found: .asciiz " not in sorted A" newline: .asciiz "\n" .text main: # ---------------------------------------------------------------------------------- # Do not modify la $s0, A # store address of array A in $s0 add $s1, $0, $0 # create variable "size" ($s1)...
Description: You will build a portfolio containing the stock prices of 20 companies. The portfolio will...
Description: You will build a portfolio containing the stock prices of 20 companies. The portfolio will be built by flipping a coin and adding a Technology company or a Manufacturing company to the portfolio based on the outcome of the coin flip. Your program will use four classes to demonstrate inheritance: Company, Technology, Manufacturer and Portfolio (the driver class). The details of these classes are outlined below. Learning Objectives: • Inheritance • Method overriding • Invoking superclass constructors You must...
1. Create a PHP program containing an multi dimensional array of all the first and last...
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order. 2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with...
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows. To support your solution, create a class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot of a one-dimensional, 20-element array. To Summarize, your application will: 1. Generate the two-dimensional array of random integers 2. Start 20 concurrent threads, each of which places the sum of one row of the two-dimensional array into the corresponding slot of a one-dimensional array. 3. Onput the index of the row with the maximum value.
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with...
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows. To support your solution, create a class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT