In: Computer Science
// -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
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