B01 - Min Array You will write a program that will ask users for the length of a
ID: 3891359 • Letter: B
Question
B01 - Min Array You will write a program that will ask users for the length of an array. Users will enter this length n. Your program will create two arrays of this length, and will ask the user for numbers until the two arrays are filled. Then, your program will create a third array of the same length. For each position i in the array, your program will assign the smallest number in the same position in the other two arrays. Then, your program will print all three arrays.
The following is an example: Length: 5 Array 1 (numbers entered by the user): [3, 0, 2, 7, 4] Array 2 (numbers entered by the user): [2, 3, 5, 3, 1] Array 3 (computed by the program): [2, 0, 2, 3, 1] Your program would print: Array 1: [3, 0, 2, 7, 4] Array 2: [2, 3, 5, 3, 1] Array 3: [2, 0, 2, 3, 1]
B02 - Counting votes Context: You will write a program that will allow users to enter electoral votes between two candidates A and B. Then, it will automatically count how many votes each candidate received and display a summary with the results. Important: In this case, first you will ask how many people will vote. For each vote, the name of the candidate will be added to the collection.
Your program needs to do the following: 1. Ask how many users will vote and then, it will display the following menu and ask the users to enter the candidate they are voting for. "Voting system. Please, enter: A - To vote for candidate A. B - To vote for candidate B. Anything else will be considered as an INVALID vote." As new votes are entered, your program needs to add the appropriate value to the collection (either the name of the candidate, or "INVALID" for invalid votes). 2. With the list of votes, your program will count how many votes each candidate received, and how many invalid votes were entered in the system. Then, it will display the results with the following format: Results: A received X votes (%). B received Y votes. (%) Z votes were invalid. (%) Total: M votes were entered in the system.
B03 - Traveling matrix Context: You will write a program that will create a 2D-array or matrix representing distances from cities. Then, for each city you will print what's the nearest city. Your program needs to: 1. Ask the user for the number of cities. 2. Create a 2D-array or matrix that will represent the distance from each city. 3. As the user for the distance between each city to fill the matrix. 4. Once your 2D-array is full, for each city, your program will print the nearest one (other than the current city).
For instance: For the below matrix, your program would then print: City A: nearest city is City B. City B: nearest city is City C City C: nearest city is City A City D: nearest city is City C
B04 - Academic system Context: You will write a program that will ask users to enter the final grades of 3 students for 4 classes. Then, your program will automatically obtain the min, max and average grade for each course and display a summary dialog with these results. Important: Your program will work with a 2-dimensional collection of fixed size. That is, your program will always ask for the grades of 3 and only 3 students, and in 4 and only 4 courses. Don't use ArrayLists or 1-D arrays.
Your program needs to: 1. Have a method to create and fill the 2-D collection of grades for 3 students in 4 classes. Your program will ask the user for these grades. The method must return the collection of grades.
2. Once the collection of grades is received, your program will call another method that will iterate course by course. For each course, the program will compute the min grade, the max grade and the average. You can use Math.min and Math.max to compute the min and max grades. Then, your program will display a dialog with the following format:
Course X:
Min grade: M
Max grade: N
Average grade: O
City A City B City C City D City A 0 2 3 5 City B 3 0 1 4 City C 1 3 0 2 City D 4 3 2 0Explanation / Answer
B01:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of array");
int n=sc.nextInt(); //size of the array and three arrays created with the specified size
int a[]= new int[n];
int b[]=new int[n];
int c[]=new int[n];
for(int i=0;i<n;i++){ // elemnts of first array
System.out.println("enter the element of array");
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++){ // elements of second array
System.out.println("enter the element of array");
b[i]=sc.nextInt();
}
for(int i=0;i<n;i++){ // solution for third array
if(a[i]<=b[i]){
c[i]=a[i];
}
else{
c[i]=b[i];
}
}
// printing all arrays
System.out.print("Array 1: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.print(" Array 2: ");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.print(" Array 3: ");
for(int i=0;i<n;i++){
System.out.print(c[i]+" ");
}
}
}
OUTPUT:
Enter the size of array
3
enter the elements of array
2
3
4
enter the elements of array
2
5
6
Array 1: 2 3 4 Array 2: 2 5 6 Array 3: 2 3 4
B02:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("How many users will vote");
int n=sc.nextInt();
int a1=0,b=0,ival=0;
int sum=0;
String a[]=new String[n];
while(sum<n){
System.out.println("Voting system. Please, enter: A - To vote for candidate A. B - To vote for candidate B. Anything else will be considered as an INVALID vote.");
char ch=sc.next().charAt(0);
switch(ch){
case 'A': a[sum]=Character.toString(ch);
break;
case 'B': a[sum]=Character.toString(ch);
break;
default: a[sum]="INVALID";
break;
}
sum++;
}
for(int i=0;i<n;i++){
if(a[i].equals("A")){
a1++;
}
else if (a[i].equals("B")){
b++;
}
else{
ival++;
}
}
System.out.println("A received "+ a1 + " votes B received "+ b + " votes "+ival+" votes were invalid. Total: "+ n + " votes were entered in the system.");
}
}
OUTPUT:
How many users will vote
3
Voting system.
Please, enter:
A - To vote for candidate A.
B - To vote for candidate B.
Anything else will be considered as an INVALID vote.
A
Voting system.
Please, enter:
A - To vote for candidate A.
B - To vote for candidate B.
Anything else will be considered as an INVALID vote.
A
Voting system.
Please, enter:
A - To vote for candidate A.
B - To vote for candidate B.
Anything else will be considered as an INVALID vote.
e
A received 2 votes
B received 0 votes
1 votes were invalid.
Total: 3 votes were entered in the system.
B03:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number of Cities");
int n=sc.nextInt();
int a[][]=new int[n][n];
int sum=0,near=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j){
a[i][j]=0;
}
else{
System.out.print("enter the distance between "+(i+1)+" --> "+(j+1)+" :");
a[i][j]=sc.nextInt();
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(a[i][j]+" ");
}
System.out.print(" ");
}
for(int i=0;i<n;i++){
sum=a[i][0];near=0;
for(int j=0;j<n;j++){
if(sum==0)
{
continue;
}
if(i==j){
continue;
}
else{
if(sum<a[i][j]){
near=j+1;
}
sum=a[i][j];
}
}
System.out.println("City "+(i+1)+" : nearest city is City "+near+".");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.