Question

please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

please answer in Java..all excercises.
/**
 * YOUR NAME GOES HERE
 * 4/7/2017
 */


public class Chapter9a_FillInTheCode
{
    public static void main(String[] args)
    {
        String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"},
                           {"Detroit", "Newark", "Boston", "Seattle"}};


        // ------> exercise 9a1
        // this code prints the element at row at index 1 and column at index 2


        // your code goes here

        System.out.println("Done exercise 9a1.\n");


        // ------> exercise 9a2
        // this code prints all the states in the first row (the row at index 0) of array goe that start with an M.
        for (int col = 0; col < geo[0].length; col++)
        {
            // your code goes here
        }

        System.out.println("Done exercise 9a2.\n");


        // ------> exercise 9a3
        // this code prints all the cities in the second row (the row at index 1) of the array geo.
        for (int col = 0; col < geo[1].length; col++)
        {
            // your code goes here
        }


        System.out.println("Done exercise 9a3.\n");

        // ------> exercise 9a4
        // this code prints all the elements of the array geo.
        for (int row = 0; row < geo.length; row++)
        {
            // your code goes here
        }


        System.out.println("Done exercise 9a4.\n");


        int [][] a = {{9, 6, 8, 10, 5},
                      {7, 6, 8, 9, 6},
                      {4, 8, 11, 5, 6}};

        // ------> exercise 9a5
        // this code calculates and prints the sum of all the elements in the array a.
        int sum = 0;
        for (int row = 0; row < a.length; row++)
        {
            // your code goes here
        }
        System.out.println("sum of all elements in array a = " + sum);
        System.out.println("Done exercise 9a5.\n");


        // ------> exercise 9a6
        // this code calculates and prints the number of times the value 8 appears in the array a.
        int numberOfEights = 0;
        for (int row = 0; row < a.length; row++)
        {
            // your code goes here
        }
        System.out.println("# of 8s in a = " + numberOfEights);
        System.out.println("Done exercise 9a6.\n");


        // ------> exercise 9a7
        // this code calculates and prints the number of times the value 6 appears in the second row
        // (i.e., the row whose index is 1) of array a.
        int numberOfSixes = 0;
            // your code goes here

        System.out.println("# of 6s in the second row = " + numberOfSixes);
        System.out.println("Done exercise 9a7.\n");

        // ------> exercise 9a8
        // this code calculates the sum of the elements in the third column (i.e, the column with index 2) of array a.
        int columnSum = 0;
        for (int row = 0; row < a.length; row++)
        {
            // your code goes here
        }
        System.out.println("sum of elements in the third column = " + columnSum);

        System.out.println("Done exercise 9a8.\n");

        // ------> exercise 9a9
        // this code calculates the sum of the elements in the second row (i.e, the row with index 1) of array a.
        int rowSum = 0;
        for (int col = 0; col < a[1].length; col++)
        {
            // your code goes here
        }
        System.out.println("sum of elements in the second row = " + rowSum);

        System.out.println("Done exercise 9a9.\n");


        // ------> exercise 9a10
        // this code calculates and prints totals per row of array a.
        for (int row = 0; row < a.length; row++)
        {
            // your code goes here
            System.out.println("sum of elements in row " + row + " = " + rowSum);
        }


        System.out.println("Done exercise 9a10.\n");

        // ------> exercise 9a11
        // this code finds and prints the largest value of array a.
        int maxValue = Integer.MIN_VALUE;
        for (int row = 0; row < a.length; row++)
        {
            // your code goes here

        }
        System.out.println("the largest value in array a = " + maxValue);
        System.out.println("Done exercise 9a11.\n");


        // ------> exercise 9a12
        // this code finds the "smallest" row of array a (the row with the smallest sum of the elements)
        // and prints the its index, its sum, and its elements 


        System.out.println("Done exercise 9a12.");

        // ------> exercise 9a13
        // this code creates a single dimensional array of boolean with the number of elements equal to the number
        // of rows in array a. Every element of the boolean array should be set to true if the sum of the elements
        // in the corresponding row in array a is greater than 35.
        // Prints the boolean array.


        System.out.println("Done exercise 9a13.");


        // ------> exercise 9a14
        // this code sets to 0 all the elements of the even-numbered rows and sets to 1 all the elements of
        // the odd-numbered rows of array a. Prints all the elements of the updated array


        System.out.println("Done exercise 9a14.");
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

public class Chapter9a_FillInTheCode {

public static void main(String[] args) {

String[][] geo = { { "MD", "NY", "NJ", "MA", "CA", "MI", "OR" }, { "Detroit", "Newark", "Boston", "Seattle" } };

// ------> exercise 9a1

// this code prints the element at row at index 1 and column at index 2

// your code goes here

System.out.println(geo[1][2]);

System.out.println("Done exercise 9a1.\n");

// ------> exercise 9a2

// this code prints all the states in the first row (the row at index 0) of

// array goe that start with an M.

for (int col = 0; col < geo[0].length; col++) {

// your code goes here

if (geo[0][col].startsWith("M"))

System.out.println(geo[0][col]);

}

System.out.println("Done exercise 9a2.\n");

// ------> exercise 9a3

// this code prints all the cities in the second row (the row at index 1) of

// the array geo.

for (int col = 0; col < geo[1].length; col++) {

// your code goes here

System.out.println(geo[1][col]);

}

System.out.println("Done exercise 9a3.\n");

// ------> exercise 9a4

// this code prints all the elements of the array geo.

for (int row = 0; row < geo.length; row++) {

// your code goes here

for (int col = 0; col < geo[row].length; col++) {

System.out.println(geo[row][col]);

}

}

System.out.println("Done exercise 9a4.\n");

int[][] a = { { 9, 6, 8, 10, 5 }, { 7, 6, 8, 9, 6 }, { 4, 8, 11, 5, 6 } };

// ------> exercise 9a5

// this code calculates and prints the sum of all the elements in the array

// a.

int sum = 0;

for (int row = 0; row < a.length; row++) {

// your code goes here

for (int col = 0; col < a[row].length; col++) {

sum += a[row][col];

}

}

System.out.println("sum of all elements in array a = " + sum);

System.out.println("Done exercise 9a5.\n");

// ------> exercise 9a6

// this code calculates and prints the number of times the value 8 appears

// in the array a.

int numberOfEights = 0;

for (int row = 0; row < a.length; row++) {

// your code goes here

for (int col = 0; col < a[row].length; col++) {

if(a[row][col]==8)

numberOfEights++;

}

}

System.out.println("# of 8s in a = " + numberOfEights);

System.out.println("Done exercise 9a6.\n");

// ------> exercise 9a7

// this code calculates and prints the number of times the value 6 appears

// in the second row

// (i.e., the row whose index is 1) of array a.

int numberOfSixes = 0;

// your code goes here

for (int col = 0; col < geo[1].length; col++) {

if(a[1][col]==6)

numberOfSixes++;

}

System.out.println("# of 6s in the second row = " + numberOfSixes);

System.out.println("Done exercise 9a7.\n");

// ------> exercise 9a8

// this code calculates the sum of the elements in the third column (i.e,

// the column with index 2) of array a.

int columnSum = 0;

for (int row = 0; row < a.length; row++) {

// your code goes here

columnSum += a[row][2];

}

System.out.println("sum of elements in the third column = " + columnSum);

System.out.println("Done exercise 9a8.\n");

// ------> exercise 9a9

// this code calculates the sum of the elements in the second row (i.e, the

// row with index 1) of array a.

int rowSum = 0;

for (int col = 0; col < a[1].length; col++) {

// your code goes here

rowSum += a[1][col];

}

System.out.println("sum of elements in the second row = " + rowSum);

System.out.println("Done exercise 9a9.\n");

// ------> exercise 9a10

// this code calculates and prints totals per row of array a.

for (int row = 0; row < a.length; row++) {

// your code goes here

rowSum = 0;

for(int col = 0; col<a[row].length; col++){

rowSum += a[row][col];

}

System.out.println("sum of elements in row " + row + " = " + rowSum);

}

System.out.println("Done exercise 9a10.\n");

// ------> exercise 9a11

// this code finds and prints the largest value of array a.

int maxValue = Integer.MIN_VALUE;

for (int row = 0; row < a.length; row++) {

// your code goes here

for(int col = 0; col<a[row].length; col++){

if(maxValue < a[row][col])

maxValue = a[row][col];

}

}

System.out.println("the largest value in array a = " + maxValue);

System.out.println("Done exercise 9a11.\n");

// ------> exercise 9a12

// this code finds the "smallest" row of array a (the row with the smallest

// sum of the elements)

// and prints the its index, its sum, and its elements

int minValue = Integer.MAX_VALUE;

int minIndex = -1;

for (int row = 0; row < a.length; row++) {

// your code goes here

rowSum = 0;

for(int col = 0; col<a[row].length; col++){

rowSum += a[row][col];

}

if(minValue > rowSum)

minValue = rowSum;

minIndex = row;

}

System.out.println("the smallest row of array a = " + minValue+", Index: "+minIndex);

System.out.println("Done exercise 9a12.");

// ------> exercise 9a13

// this code creates a single dimensional array of boolean with the number

// of elements equal to the number

// of rows in array a. Every element of the boolean array should be set to

// true if the sum of the elements

// in the corresponding row in array a is greater than 35.

// Prints the boolean array.

boolean b[] = new boolean[a.length];

for (int row = 0; row < a.length; row++) {

rowSum = 0;

for(int col = 0; col<a[row].length; col++){

rowSum += a[row][col];

}

if(rowSum>35)

b[row] = true;

else

b[row] = false;

}

for(int i=0; i<b.length; i++){

System.out.print(b[i]+" ");

}

System.out.println();

System.out.println("Done exercise 9a13.");

// ------> exercise 9a14

// this code sets to 0 all the elements of the even-numbered rows and sets

// to 1 all the elements of

// the odd-numbered rows of array a. Prints all the elements of the updated

// array

for (int row = 0; row < a.length; row++) {

for(int col = 0; col<a[row].length; col++){

if(row%2==0)

a[row][col] = 0;

else

a[row][col] = 1;

}

}

for (int row = 0; row < a.length; row++) {

for(int col = 0; col<a[row].length; col++){

System.out.print(a[row][col]+" ");

}

}

System.out.println();

System.out.println("Done exercise 9a14.");

}

}

Add a comment
Know the answer?
Add Answer to:
please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final...

    JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final int DEFAULT_CAPACITY = 2;   //default initial capacity / minimum capacity    private T[] data;   //underlying array    // ADD MORE PRIVATE MEMBERS HERE IF NEEDED!       @SuppressWarnings("unchecked")    public SmartArray(){        //constructor        //initial capacity of the array should be DEFAULT_CAPACITY    }    @SuppressWarnings("unchecked")    public SmartArray(int initialCapacity){        // constructor        // set the initial capacity of...

  • COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]]...

    COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]] is the minimum    * of the set S={a[i][index[i]] for every i such that index[i]<a[i].length}    * If the set S is empty, returns -1    * Runs in time a.length.    *    * NOTE: normally this would be private, but leave it    * public so we can test it separately from your merge.    */    public static int argMin(int [][]...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous...

    Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous comments alreadys displayed On Top of Code write summary of what code will do in comments At the end of the code have the output in comment form I Will thumbs up good Work thanks! public class MagicSquare { static int[][] createMagicSquare(int square[][]) { // Initialize position for 1 int i = 3/2; int j = 3-1; // One by one put all values...

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT