Commit c2d39668 by Adam Gerber

Revert "Revert create branch lab01 and start here

parent 70a87e79
...@@ -13,20 +13,7 @@ public class AlternateEmphasis { ...@@ -13,20 +13,7 @@ public class AlternateEmphasis {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//ask the user for input
System.out.println("Enter a sentence please:");
String[] strWords = in.nextLine().split(" ");
int nCount = 0;
for (String strWord : strWords) {
if (nCount % 2 == 0){
System.out.print(strWord.toUpperCase() + " ");
}
else {
System.out.print(strWord.toLowerCase() + " ");
}
nCount++;
}
} }
......
...@@ -10,106 +10,8 @@ public class BinaryToDigital { ...@@ -10,106 +10,8 @@ public class BinaryToDigital {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
//while (true) infinite loop
//ask the user for input (a string of one,zeros, and spaces representing a binary number)
//if the input is 'exit'
//print out "Thanks for playing. Good bye."
//break
//validate() that the input string is a valid binary with 8, 16, 32, or 64 bits represented not including any spaces
//if the input is not valid
//print out "input is not a valid binary... (and then explain the rules.)"
//continue
//processString()
//take the validated above string as parameter
//iterate; start from the last character and work backwards
//initialize an int nPow to zero
//for each char in string (iterate over the string backwards)
//if char is not valid (1, 0 or space)
//throw an exception
//if the char is 1 or zero
//if 1
//increment a result by 2^nPow
//increment nPow
//return result
String strValue;
Scanner in = new Scanner(System.in);
do { }
System.out.println("Enter an string that represents a byte in binary" +
" : (or type exit to quit.)");
strValue = in.nextLine();
if (strValue.equals("exit")) {
System.out.println("Thanks for playing. Good bye.");
break;
}
try{
System.out.println(convertToNumeric(strValue));
}
catch(Exception e){
e.printStackTrace(System.out);
System.out.print(e.getMessage() + "try again; here's an example \"0101 1111\"");
System.out.println(" or type exit to quit." );
continue;
}
} while(true); //infinite loop. Will only break upon entering 'exit'
}
private static long convertToNumeric(String sParam){
//processString()
//take the validated above string as parameter
//iterate; start from the last character and work backwards
//initialize an int nPow to zero
//initialize a long lResult to zero
int nPow = 0;
long lResult = 0;
String strChar;
//for each char in string (iterate over the string backwards) and exclude the sign bit
for(int nPos = sParam.length(); nPos > 0; nPos--) {
//can't have more 0 to 6 (the 7th is the sign bit) will not read the rest if string contains too many zeros or ones.
if (nPow > 7 )
throw new NumberFormatException("Too many ones or zeros for this to be a binary.");
//if char is either
strChar = sParam.substring(nPos - 1, nPos);
if(!strChar.equals("0") && !strChar.equals("1") && !strChar.equals(" "))
throw new NumberFormatException("You can only use zeros ones and spaces.");
if (strChar.equals("0") || strChar.equals("1")){
//check for sign bit
if(!strChar.equals("0") && nPos == 1 && sParam.length() > 1)
throw new NumberFormatException("The sign bit must be zero.");
if (strChar.equals("1")){
lResult += (long) Math.pow(2, nPow );
}
nPow++;
}
}
return lResult;
}
} }
...@@ -7,141 +7,8 @@ public class ControlStructures { ...@@ -7,141 +7,8 @@ public class ControlStructures {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
//nestedLoops();
String[] strPeeps = new String[] {"Gary", "Sally", "Aaron", "John", "Sue"};
StringTokenizer stoPeeps = new StringTokenizer("Dan Xavier Jason Mary Brad");
System.out.println(loopsFor(strPeeps));
System.out.println(loopsWhile(stoPeeps));
switchGradeComments('A');
System.out.println(controlIfExample("Adam"));
nestedLoops();
} }
private static void switchGradeComments(char cGrade){
switch (cGrade) {
case 'A' : System.out.print("Excellent");
break;
case 'B' : System.out.print("Good");
break;
case 'C' : System.out.print("Fair");
break;
case 'D' : System.out.print("Poor");
break;
case 'F' : System.out.print("Fail");
break;
default : System.out.print("Incomplete");
}//end switch
}
private static String controlIfExample(String strFirstName){
if(strFirstName.equals("Adam"))
return "Hello" + strFirstName + "Great first name!";
else
return "Hello" + strFirstName;
}
private static String controlIfElseExample(int nEyeSight){
if(nEyeSight > 2090)
return "Please sit in front rows.";
else if (nEyeSight > 2050)
return "Please sit in middle rows.";
else if (nEyeSight > 2030)
return "Please sit toward the back.";
else
return "Please sit in the back row.";
}
private static String controlTernary(boolean bFanOfThaiFood){
return (bFanOfThaiFood ? "Love the spread": "yuck");
}
private static String loopsFor(String[] strStudents){
//find the person who is first on the roster
//assume he/she is the first one.
String strFirst = strStudents[0];
for (int nC = 1; nC < strStudents.length; nC++){
if (strStudents[nC].compareTo(strFirst) <= 0)
strFirst = strStudents[nC];
}
return strFirst;
}
private static String loopsWhile(StringTokenizer stoStudents){
//last on roster
String strLast = stoStudents.nextToken();
String strNext;
while(stoStudents.hasMoreTokens()){
strNext = stoStudents.nextToken();
if (strNext.compareTo(strLast) >= 0)
strLast = strNext;
}
return strLast;
}
private static void loopsBreakContinue(){
int nCount = 0;
//infinite loop
while(true) {
nCount++;
if(nCount == 510)
break; // Out of loop
if(nCount % 10 != 0)
continue; // Top of loop
System.out.println(nCount);
}
}
private static void nestedLoops(){
//odometer
for(int nA = 0; nA < 10; nA++)
for(int nB = 0; nB < 10; nB++)
for(int nC = 0; nC < 10; nC++)
System.out.println(nA + "|" + nB + "|" + nC);
}
} }
...@@ -19,149 +19,7 @@ public class DigitalToBinary { ...@@ -19,149 +19,7 @@ public class DigitalToBinary {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
}
//do loop
//ask the user for some byte value or type exit to quit
//if the value is 'exit'
//"Thank you for playing"
//break
//convert that String value to a Byte (handle any exceptions)
//processValue() and print out results
//processValue()
//given the bitLength of a byte and the value
//build a string representation; use String[] length of Byte.SIZE
//set the strChars[bitLength -1] = "0"
//for some counter init to bitLength - 2 and decremented to zero inclusive
//if the value is greater than= 2^counter
//add "1" to the String Array at that location
//subtract 2^counter from the value
//else
//add "0" to the String Array
//iterate the array while array index < length of array
//if divisible by 4,
//then space + index value
//else
//index value
//return string
Scanner scn = new Scanner(System.in);
//alt is public Scanner(File source) throws FileNotFoundException
String sValue;
do {
System.out.println("Enter an integer between 0 and 127 to convert from digital" +
" to binary: (or type exit to quit.)");
sValue = scn.nextLine();
if (sValue.equals("exit")) {
System.out.println("Thanks for playing. Good bye.");
break;
}
try{
//this could throw an execption
byte yNum = Byte.parseByte(sValue);
if (yNum < 0)
throw new NumberFormatException("Your byte is negative, sorry positive numbers only.");
System.out.println(convertDigitalToBinary(Byte.SIZE, Byte.parseByte(sValue)));
}
catch(Exception e){
e.printStackTrace(System.out);
System.out.println("try again..or type exit to quit. Error:" + e.getMessage());
continue;
}
} while(true); //infinite loop. Will only break upon entering 'exit'
}
//given the bitLength of the primitive type and the value
/**
*
* @param nBitLength the length of the integer type that you pass in
* @param lValue value of the string you want to convert to binary
* @return returns a string formatted with spaces representing a binary number
*/
public static String convertDigitalToBinary(int nBitLength, long lValue){
//build a string representation; possible prims are byte, short, int, and long
//define some variables; the string to output/return; a String[]
String strOut = "";
String[] strChars = new String[nBitLength];
strChars[nBitLength - 1] = "0";
//for some counter init to bitLength - 1 and decremented to zero inclusive
for(int nCounter = nBitLength - 2; nCounter >= 0; nCounter--){
//if the value is greater than= 2^counter
if(lValue >= (long)Math.pow(2, nCounter) ){
//add "1" to the String Array at that location
strChars[nCounter] = "1";
//subtract 2^counter from the value
lValue -= (long)Math.pow(2, nCounter);
}
//else
else {
//add "0" to the String Array
strChars[nCounter] = "0";
}
}
//add some spaces --for maximum effect
for(int nC =0; nC < strChars.length; nC++ ){
//if divisible by 4,
if(nC % 4 == 0){
strOut =strChars[nC] + " " + strOut ;
}
else {
strOut =strChars[nC] + strOut ;
}
}
//return string
return strOut;
}
......
...@@ -8,24 +8,6 @@ public class EnterSomething { ...@@ -8,24 +8,6 @@ public class EnterSomething {
public static void main(String[] args) { public static void main(String[] args) {
//declare a string to hold input
//while input != exit
//ask for input and give instructions
//print Echo + input
Scanner in = new Scanner(System.in);
String strInput;
do
{
System.out.println("Enter something: (or exit to quit)");
strInput = in.next();
System.out.println("Echo:" + strInput );
} while(!strInput.equals("exit"));
} }
......
...@@ -6,36 +6,7 @@ public class PromotionAndCastingPrimitives { ...@@ -6,36 +6,7 @@ public class PromotionAndCastingPrimitives {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
double dOperand1 = 5.897;
int nOperand2 = 8;
//promotion works when:
//there are two operands of different primtive types; the smaller precision operand will get promoted to larger precision operand
double dResult = dOperand1 / nOperand2; //here nOperand2 is promoted to double
//the primitive operand on the left of the assignment operator is of greater precision than the result of the right expression
//in this case, the result of integer division is 2.66666 trucated to 2, then the result is assigned as (2.0)
float fResult = nOperand2 / 3;
//you concatenate a string with a primitive, the primitive is promoted to a string
String strResult = "Hello" + dOperand1;
System.out.println(dResult);
System.out.println(fResult);
System.out.println(strResult);
//you can downcast a primitive.
//upcasting happens automatically; it's called promotion and you lose no precision
//downcasting loses precsion
int nAnswer1 = (int)56.9356974;
//long lAnswer1 = Math.max(3.856, 98798798);
long lAnswer1 = Math.max((long)3654.856, 568211);
//you must cast and casting primitives sometimes loses precision
System.out.println(nAnswer1);
System.out.println(lAnswer1);
} }
} }
...@@ -9,62 +9,6 @@ public class RockPaperScissors3 ...@@ -9,62 +9,6 @@ public class RockPaperScissors3
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
Scanner scan = new Scanner(System.in);
System.out.println("Player 1: Choose rock, scissors, or paper:");
String strPlayer1 = scan.next().toLowerCase();
System.out.println("Player 2: Choose rock, scissors, or paper:");
String strPlayer2 = scan.next().toLowerCase();
switch (strPlayer1){
case "rock":
switch (strPlayer2){
case "rock":
System.out.println("Tie");
break;
case "paper":
System.out.println("Player two wins: " + strPlayer2 + " beats " + strPlayer1);
break;
case "scissors":
System.out.println("Player one wins: " + strPlayer1 + " beats " + strPlayer2);
break;
default:
System.out.println("can't play "+ strPlayer2);
}
break;
case "paper":
switch (strPlayer2){
case "rock":
System.out.println("Player one wins: " + strPlayer1 + " beats " + strPlayer2);
break;
case "paper":
System.out.println("Tie");
break;
case "scissors":
System.out.println("Player two wins: " + strPlayer2 + " beats " + strPlayer1);
break;
default:
System.out.println("can't play "+ strPlayer2);
}
break;
case "scissors":
switch (strPlayer2){
case "rock":
System.out.println("Player two wins: " + strPlayer2 + " beats " + strPlayer1);
break;
case "paper":
System.out.println("Player one wins: " + strPlayer1 + " beats " + strPlayer2);
break;
case "scissors":
System.out.println("Tie");
break;
default:
System.out.println("can't play "+ strPlayer2);
}
break;
default:
System.out.println("can't play "+ strPlayer1);
}
} }
} }
\ No newline at end of file
...@@ -6,24 +6,6 @@ public class TypeConversion { ...@@ -6,24 +6,6 @@ public class TypeConversion {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
int nNumber = Integer.parseInt("5");
double dNumber = Double.parseDouble("8.18");
byte yNumber = Byte.parseByte("127");
boolean bFlag = Boolean.parseBoolean("TruE");
System.out.println(nNumber);
System.out.println(dNumber);
System.out.println(yNumber);
System.out.println(bFlag);
} }
......
...@@ -11,11 +11,5 @@ public class MilkJarCalculator ...@@ -11,11 +11,5 @@ public class MilkJarCalculator
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
double milk = 5.5; // gallons
double jarCapacity = 0.75; // gallons
//apply the cast here
int completelyFilledJars = (int) (milk / jarCapacity);
System.out.println(completelyFilledJars);
} }
} }
\ No newline at end of file
...@@ -14,47 +14,6 @@ public class NumberDemo ...@@ -14,47 +14,6 @@ public class NumberDemo
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int aInt = 23;
byte aByte = +127;
short aShort = 32767;
long aLong = 123456789012345678L; // L indicates a long value
double aDouble = 3.456789D; // D indicates a double value
float aFloat = -320.00987F; // F indicates a float value
char aChar = 97;
System.out.println("aInt = " + aInt);
System.out.println("aByte = " + aByte);
System.out.println("aShort = " + aShort);
System.out.println("aLong = " + aLong);
System.out.println("aDouble = " + aDouble);
System.out.println("aFloat = " + aFloat);
System.out.println("aChar = " + aChar);
aInt = 44;
aByte = -128;
aShort = -32768;
aLong = -33333333333L;
aDouble = 3.32E-6;
aFloat = 4.00007F;
aChar = 98;
System.out.println("aInt = " + aInt);
System.out.println("aByte = " + aByte);
System.out.println("aShort = " + aShort);
System.out.println("aLong = " + aLong);
System.out.println("aDouble = " + aDouble);
System.out.println("aFloat = " + aFloat);
System.out.println("aChar = " + aChar);
final int aConstantInt = 23;
final byte aConstantByte = +127;
final short aConstantShort = 32767;
final long aConstantLong = 123456789012345678L; // L indicates a long value
final double aConstantDouble = 3.456789D; // D indicates a double value
final float aConstantFloat = -320.00987F; // F indicates a float value
final char aConstantChar = 97;
System.out.println("aConstantInt = " + aConstantInt);
System.out.println("aConstantByte = " + aConstantByte);
System.out.println("aConstantShort = " + aConstantShort);
System.out.println("aConstantLong = " + aConstantLong);
System.out.println("aConstantFloat = " + aConstantFloat);
System.out.println("aConstantChar = " + aConstantChar);
// aConstantInt = 24; // A compile-time error when uncommented
} }
} }
\ No newline at end of file
...@@ -12,55 +12,7 @@ public class RockPaperScissors ...@@ -12,55 +12,7 @@ public class RockPaperScissors
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
Scanner scan = new Scanner(System.in);
System.out.println("Player 1: Choose rock, scissors, or paper:");
String player1 = scan.next().toLowerCase();
System.out.println("Player 2: Choose rock, scissors, or paper:");
String player2 = scan.next().toLowerCase();
if (player1.equals("rock"))
{
if (player2.equals("rock"))
{
System.out.println("It is a tie.");
}
if (player2.equals("scissors"))
{
System.out.println("Player 1 wins.");
}
if (player2.equals("paper"))
{
System.out.println("Player 2 wins.");
}
}
else if (player1.equals("scissors"))
{
if (player2.equals("rock"))
{
System.out.println("Player 2 wins.");
}
if (player2.equals("scissors"))
{
System.out.println("It is a tie.");
}
if (player2.equals("paper"))
{
System.out.println("Player 1 wins.");
}
}
else if (player1.equals("paper"))
{
if (player2.equals("rock"))
{
System.out.println("Player 1 wins.");
}
if (player2.equals("scissors"))
{
System.out.println("Player 2 wins.");
}
if (player2.equals("paper"))
{
System.out.println("It is a tie.");
}
}
} }
} }
\ No newline at end of file
...@@ -11,46 +11,7 @@ public class RockPaperScissors2 ...@@ -11,46 +11,7 @@ public class RockPaperScissors2
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
Scanner scan = new Scanner(System.in);
System.out.println("Player 1: Choose rock, scissors, or paper:");
String player1 = scan.next().toLowerCase();
System.out.println("Player 2: Choose rock, scissors, or paper:");
String player2 = scan.next().toLowerCase();
if (player1.equals("rock") && player2.equals("rock"))
{
System.out.println("It is a tie.");
}
if (player1.equals("rock") && player2.equals("scissors"))
{
System.out.println("Player 1 wins.");
}
if (player1.equals("rock") && player2.equals("paper"))
{
System.out.println("Player 2 wins.");
}
if (player1.equals("scissors") && player2.equals("rock"))
{
System.out.println("Player 2 wins.");
}
if (player1.equals("scissors") && player2.equals("scissors"))
{
System.out.println("It is a tie.");
}
if (player1.equals("scissors") && player2.equals("paper"))
{
System.out.println("Player 1 wins.");
}
if (player1.equals("paper") && player2.equals("rock"))
{
System.out.println("Player 1 wins.");
}
if (player1.equals("paper") && player2.equals("scissors"))
{
System.out.println("Player 2 wins.");
}
if (player1.equals("paper") && player2.equals("paper"))
{
System.out.println("It is a tie.");
}
} }
} }
...@@ -9,19 +9,7 @@ public class StringEqual ...@@ -9,19 +9,7 @@ public class StringEqual
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
String str1 = "abcd";
String str2 = "abcdefg";
String str3 = str1 + "efg";
System.out.println("str2 = " + str2);
System.out.println("str3 = " + str3);
//this is comparing memory addresses
if (str2 == str3)
{
System.out.println("The strings are equal");
}
else
{
System.out.println("The strings are not equal");
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment