Question

In: Computer Science

// -Type in your name in the space provided above in this comment header. // -Fill...

// -Type in your name in the space provided above in this comment header.
// -Fill in the code that will perform the actions. See the comments
//   (in the code) that describes what to do.

// -The output must match the output given to you by the instructor.
// -After the last brace, in this code on a new line, make a comment
//   stating your name and what your major is at PSU.
//=====================================================================

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;

public class W7_InClass_Lab
{

static ArrayList<String> players = new ArrayList<String>(Arrays.asList
("Frank", "Chase", "Ryan", "Carlos", "Cole", "Jimmy"));

//---------------------------------------------------------------------
public static void main(String[] args)
   {
   int n, idx;
   boolean itWorked;
   Scanner kb = new Scanner (System.in);
     
   System.out.println ("\n(1-6): Testing student knowldege with ArrayLists.");
   System.out.println ("Before: " + players.toString());


   //-- (1) Check to see if "Carlos" is on the list. Display a msg
   //-- stating whether he is in the list or not.

   //-- (2) Remove "Frank" from list.

   //-- (3) Add "Pedro" to the list, before "Jimmy".

   //-- (4) Add "Shane" to the end of list.

   //-- (5) Find out how many players are in the list.

   //-- (6) Print out the Array List with all of the changes.
   System.out.println ("After: " + players.toString());   // xxxx

   //-- (7) Use the code below that converts an integer to binary.
   //--   Add code so there is a spave between every 8 bits, to make the
   //--   output more redable.

   String binaryStr = "";
   int saveInt, intValue, quotient, remainder;
   int cnt = 0;
  
   System.out.println ("\n(7) Converting an unsigned Integer to binary.");
   System.out.print ("Enter an integer value: ");
   intValue = kb.nextInt();

   if (intValue >= 0)
       {
       saveInt = intValue;

       while (intValue > 0)
           {
           remainder = intValue % 2;
           intValue = intValue / 2;
           binaryStr = (char)(remainder+48) + binaryStr;
           }
       System.out.printf ("After Conversion: %d(10) = %s(2).\n", saveInt, binaryStr);
       }

   //-- (8) Use the array called grades, below.
   //--   starting with index 0m use a for loop to change every other
   //--   grade to 100.
   //--   Printf what the array was BEFORE the changes, and AFTER then Changes.
      
   System.out.println ("\n(8) Laying with an array");      

   int grades[] = { 87, 95, 65, 70, 77, 99, 0, 65, 25, 80, 90, 11 };


   //-- (9) Don't allow the quadratic formula below to crash.
   //-- Add checks for the two possible ways the formula can crash.
   //-- Print out different msgs stating what the problem is,
   //--   Otherwise do the calculations
              
   double a, b, c;
   double underRadical, denominator, answer1, answer2;
  
   System.out.println ("\n(9)Calculating the quadratic formula: ");
   System.out.print ("Enter a value for a: ");
   a = kb.nextDouble();
   System.out.print ("Enter a value for b: ");
   b = kb.nextDouble();
   System.out.print ("Enter a value for c: ");
   c = kb.nextDouble();

   underRadical = (b * b) - (4 * a * c);
   denominator = 2 * a;
  
   answer1 = ((-1.0 * b) - Math.sqrt(underRadical)) / denominator;
   answer2 = ((-1.0 * b) + Math.sqrt(underRadical)) / denominator;
   System.out.println();
   System.out.printf ("Quad.Form., negative answer = %.4f.\n", answer1);
   System.out.printf ("Quad.Form., positive answer = %.4f.\n", answer2);
   } // end-main
} // end-class

Solutions

Expert Solution

Hi, I have added the code that have been asked. It is running fine. I am attaching the code below. Please name the file"W7_InClass_Lab.java".

Please rate satisfied.

Thanks.


// -Type in your name in the space provided above in this comment header.
// -Fill in the code that will perform the actions. See the comments
//   (in the code) that describes what to do.

// -The output must match the output given to you by the instructor.
// -After the last brace, in this code on a new line, make a comment
//   stating your name and what your major is at PSU.
//=====================================================================

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;

public class W7_InClass_Lab
{

static ArrayList<String> players = new ArrayList<String>(Arrays.asList
("Frank", "Chase", "Ryan", "Carlos", "Cole", "Jimmy"));

//---------------------------------------------------------------------


public static void main(String[] args)
   {
   int n, idx;
   boolean itWorked;
   Scanner kb = new Scanner (System.in);
     
   System.out.println ("\n(1-6): Testing student knowldege with ArrayLists.");
   System.out.println ("Before: " + players.toString());


   //-- (1) Check to see if "Carlos" is on the list. Display a msg
   //-- stating whether he is in the list or not.
    if (players.contains("Carlos")) {
        System.out.println("Carlos is present in the list.");
    }
    else{
        System.out.println("Carlos is not present in the list.");
    }

   //-- (2) Remove "Frank" from list.
   players.remove("Frank");

   //-- (3) Add "Pedro" to the list, before "Jimmy".
   int pos = players.indexOf("Jimmy");
   players.add(pos, "Pedro");

   //-- (4) Add "Shane" to the end of list.
   players.add("Shane");

   //-- (5) Find out how many players are in the list.
   int size = players.size();
   System.out.println("There are " + size + " players in the list."); 

   //-- (6) Print out the Array List with all of the changes.
   System.out.println ("After: " + players.toString());   // xxxx

   //-- (7) Use the code below that converts an integer to binary.
   //--   Add code so there is a spave between every 8 bits, to make the
   //--   output more redable.

   String binaryStr = "";
   int saveInt, intValue, quotient, remainder;
   int cnt = 0;
  
   System.out.println ("\n(7) Converting an unsigned Integer to binary.");
   System.out.print ("Enter an integer value: ");
   intValue = kb.nextInt();

   if (intValue >= 0){
        saveInt = intValue;
        int count = 0; 
        while (intValue > 0){
           remainder = intValue % 2;
           intValue = intValue / 2;
           count++;
           if(count==8){
               count = 0;
               binaryStr = " " + (char)(remainder+48) + binaryStr;
           }
           else binaryStr = (char)(remainder+48) + binaryStr;
        }
        
        System.out.printf ("After Conversion: %d(10) = %s(2).\n", saveInt, binaryStr);
    }

   //-- (8) Use the array called grades, below.
   //--   starting with index 0m use a for loop to change every other
   //--   grade to 100.
   //--   Printf what the array was BEFORE the changes, and AFTER then Changes.
      
   System.out.println ("\n(8) Laying with an array");      

   int grades[] = { 87, 95, 65, 70, 77, 99, 0, 65, 25, 80, 90, 11 };
   int newgrades[] = new int[grades.length];
   int i;
   for(i=0; i<grades.length; i++){
       if(i==8) newgrades[i]=grades[i];
       else newgrades[i]=100;
   }
   
   System.out.printf("Before grades : " + Arrays.toString(grades));
   System.out.printf("After grades : " + Arrays.toString(newgrades));


   //-- (9) Don't allow the quadratic formula below to crash.
   //-- Add checks for the two possible ways the formula can crash.
   //-- Print out different msgs stating what the problem is,
   //--   Otherwise do the calculations
              
   double a, b, c;
   double underRadical, denominator, answer1=0, answer2=0;
  
   System.out.println ("\n(9)Calculating the quadratic formula: ");
   System.out.print ("Enter a value for a: ");
   a = kb.nextDouble();
   System.out.print ("Enter a value for b: ");
   b = kb.nextDouble();
   System.out.print ("Enter a value for c: ");
   c = kb.nextDouble();

   underRadical = (b * b) - (4 * a * c);
   denominator = 2 * a;
   if(underRadical<0){
       System.out.println("The roots of the equation will be imaginary.");
   }
   else if(a==0){
       System.out.println("Since a is 0, the given equation is not quadratic.");
   }
   else{
       answer1 = ((-1.0 * b) - Math.sqrt(underRadical)) / denominator;
       answer2 = ((-1.0 * b) + Math.sqrt(underRadical)) / denominator;
   }
   System.out.println();
   System.out.printf ("Quad.Form., negative answer = %.4f.\n", answer1);
   System.out.printf ("Quad.Form., positive answer = %.4f.\n", answer2);
   } // end-main
}; // end-class

Related Solutions

Fill in the last column by adding the name of the test that fits the provided...
Fill in the last column by adding the name of the test that fits the provided information List and define the steps of a hypothesis test. Then, describe an example where you use those steps. Steps Your Example Hypothesis Level of Significance Data Statistic Test Conclusion Interpretation
Your spaceship has docked at a space station above Mars. The temperature inside the space station...
Your spaceship has docked at a space station above Mars. The temperature inside the space station is a carefully controlled 24 ∘C at a pressure of 735 mmHg . A balloon with a volume of 515 mL drifts into the airlock where the temperature is -83 ∘C and the pressure is 0.120 atm . What is the final volume, in milliliters, of the balloon if n remains constant and the balloon is very elastic?
Create a new file name condition_quiz.py. Add a comment with your name and the date. Prompt...
Create a new file name condition_quiz.py. Add a comment with your name and the date. Prompt the user to enter the cost. Convert the input to a float. Prompt the user for a status. Convert the status to an integer Compute the special_fee based on the status. If the status is 0, the special_fee will be 0.03 of the cost. Else if the status is 1, the special_fee will be 0.04 of the cost. Else if the status is 2,...
python Create a new file name condition_quiz.py. Add a comment with your name and the date....
python Create a new file name condition_quiz.py. Add a comment with your name and the date. Prompt the user to enter the cost. Convert the input to a float. Prompt the user for a status. Convert the status to an integer Compute the special_fee based on the status. If the status is 0, the special_fee will be 0.03 of the cost. Else if the status is 1, the special_fee will be 0.04 of the cost. Else if the status is...
In the blank space provided for each type of “accounting change” or “transaction correction” listed below,...
In the blank space provided for each type of “accounting change” or “transaction correction” listed below, indicate whether the change/correction is: Change in Accounting Principle, Change in Accounting Estimate, Change in Reporting Entity, or Error Correction. a. Change in the estimating rates and procedures used to determine a company’s estimated pension liability: _______________________________________________. b. Change from FIFO to LIFO method of inventory valuation: ________________ ______________________________. c. Change from reporting a single company’s financials to reporting consolidated financials resulting from the...
. Based on your literature findings (above), critically evaluate the comment that ‘traditional budgeting practices are...
. Based on your literature findings (above), critically evaluate the comment that ‘traditional budgeting practices are constraint on creativity, and the time and energy spent on budget formulation is better spent elsewhere’.
In the space below, fill in the blank space with a cause of economic growth? (one...
In the space below, fill in the blank space with a cause of economic growth? (one or a few words are sufficient) "An increase in __________ will cause more economic growth." "An increase in __________ will cause more economic growth." "An increase in __________ will cause more economic growth." "An increase in __________ will cause more economic growth." In the space below, fill in the blank space with a cause of economic growth? (different than the ones above) "A decrease...
Show all of your work in the space provided. Show all of your work to get...
Show all of your work in the space provided. Show all of your work to get full credit. All answers have to be in SI units(20 pints for each questions). 2.Following are the two sets of position and time data collected using a motion sensor and cart.        Table 1. table 2 Time (s) Position (m) V(instantaneous) (m/s) 0 10.0 0.25 13.0 0.50 16.5 0.75 21.0 1.00 26.5 1.25 32.3 1.50 38.0 1.75 44.5 2.00 52.0 2.25 60.0 2.50 68.5...
Comment on whether it is fair or unfair of the courts to do the above and...
Comment on whether it is fair or unfair of the courts to do the above and explain why 2) Explain how knowing the court’s approach to professional liability should help someone who intends to practice in an occupation where liability is potentially an issue (i.e. why is it essential that professionals keep up-to-date with changes in their occupation
Your are given with the Audit report, Identify the type of Audit report, also comment in...
Your are given with the Audit report, Identify the type of Audit report, also comment in few words about the Auditor’s opinion. ? Audit Report We have audited the annexed balance sheet of PAKISTAN INTERNATIONAL AIRLINES CORPORATION (the Corporation) as at December 31, 2007 and the related profit and loss account, cash flow statement and statement of changes inequity together with the notes forming part thereof, for the year then ended and we state that we have obtained all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT