From 01edbac47993ad89f962d12c5ec40ff8b7273b9d Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 13:51:27 -0500 Subject: [PATCH 01/22] Added comment --- ch02/Variables.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..46c1918 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -1,10 +1,12 @@ /** * Examples from Chapter 2. */ -public class Variables { - - public static void main(String[] args) { +public class Variables +{ + public static void main(String[] args) + { + //Luke String message; int x; @@ -59,7 +61,7 @@ public static void main(String[] args) { System.out.println(0.1 * 10); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 - + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); + + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); double balance = 123.45; // potential rounding error int balance2 = 12345; // total number of cents From 389a049d46d9b12f6453ad2d35d8ca305b00008e Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 14:49:35 -0500 Subject: [PATCH 02/22] Exercise 2-ES-1 (Java in Easy Steps) --- ch02/Arithmetic.java | 20 ++++++++++++++++++++ ch02/Assignment.java | 24 ++++++++++++++++++++++++ ch02/Constants.java | 26 ++++++++++++++++++++++++++ ch02/DataTypes.java | 17 +++++++++++++++++ ch02/FirstVariable.java | 10 ++++++++++ 5 files changed, 97 insertions(+) create mode 100644 ch02/Arithmetic.java create mode 100644 ch02/Assignment.java create mode 100644 ch02/Constants.java create mode 100644 ch02/DataTypes.java create mode 100644 ch02/FirstVariable.java diff --git a/ch02/Arithmetic.java b/ch02/Arithmetic.java new file mode 100644 index 0000000..f362561 --- /dev/null +++ b/ch02/Arithmetic.java @@ -0,0 +1,20 @@ +public class Arithmetic +{ + public static void main( String[] args) + { + int num = 100; + int factor = 20; + int sum = 0; + + sum =num + factor; //100+20 + System.out.println( "Addition sum: "+ sum); + sum = num- factor; //100-20 + System.out.println("Subtraction sum: "+ sum); + + sum = num*factor; //100x20 + System.out.println("Multiplication sum: "+ sum); + sum = num/factor; //100/20 + System.out.println("Division sum: " + sum); + + } +} diff --git a/ch02/Assignment.java b/ch02/Assignment.java new file mode 100644 index 0000000..6406a8f --- /dev/null +++ b/ch02/Assignment.java @@ -0,0 +1,24 @@ +public class Assignment +{ + public static void main(String[] args) + { + String txt= "Fantastic "; + String lang= "Java"; + txt+=lang; //Assign concatenated String + System.out.println("Add & Assign Strings: " + txt); + + int sum = 10; + int num = 20; + sum +=num; //Assign result (10+20=30) + System.out.println("Add & Assign Integers: " + sum); + + int factor = 5; + sum *= factor; // Assign result (30x5= 150) + System.out.println("Multiplication sum: " + sum); + + sum /= factor; //Assign result (150/5=30) + System.out.println("Division sum: " +sum); + + + } +} diff --git a/ch02/Constants.java b/ch02/Constants.java new file mode 100644 index 0000000..cfd65ef --- /dev/null +++ b/ch02/Constants.java @@ -0,0 +1,26 @@ +/* A program to demonstrate constant varibles. + + */ + +public class Constants +{ + public static void main (String[] args) + { + //Constant score values. + final int TOUCHDOWN= 6; + final int CONVERSION= 1; + final int FIELDGOAL= 3; + + //Calculate points scored. + int td, pat, fg, total; + + //Output calculated total. + td = 4*TOUCHDOWN; + pat=3*CONVERSION; + fg=2*FIELDGOAL; + total= (td+pat+fg); + + System.out.println("Score: "+ total); + + } +} diff --git a/ch02/DataTypes.java b/ch02/DataTypes.java new file mode 100644 index 0000000..a6c6d1a --- /dev/null +++ b/ch02/DataTypes.java @@ -0,0 +1,17 @@ +public class DataTypes +{ + public static void main ( String[] args ) + { + char letter= 'M'; + String title= "Java in easy steps"; + int number= 365; + float decimal= 98.6f; + boolean result= true; + + System.out.println("Initial is " + letter); + System.out.println("Book is " + title); + System.out.println("Days are " + number); + System.out.println("Temperature is " + decimal); + System.out.println("Answer is " + result); + } +} diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java new file mode 100644 index 0000000..07621b7 --- /dev/null +++ b/ch02/FirstVariable.java @@ -0,0 +1,10 @@ +public class FirstVariable +{ + public static void main(String[] args) + { + String message = "Initial value"; + System.out.println(message); + message = "Modified value"; + System.out.println(message); + } +} From a0096d0dd66b04e793da3281c8e427586a2725c0 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 14:55:03 -0500 Subject: [PATCH 03/22] Exercise 2-ES-1 (Java in Easy Steps) Ex. 1-5 --- ch02/.idea/vcs.xml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 ch02/.idea/vcs.xml diff --git a/ch02/.idea/vcs.xml b/ch02/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch02/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From ac7de8e041509172a0a1ce88d176633ab2168ed9 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 14:57:16 -0500 Subject: [PATCH 04/22] Exercise 2-ES-1 (Java in Easy Steps) Ex. 1-5 --- ch02/.idea/misc.xml | 6 ++++++ ch02/.idea/modules.xml | 8 ++++++++ ch02/ch02.iml | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 ch02/.idea/misc.xml create mode 100644 ch02/.idea/modules.xml create mode 100644 ch02/ch02.iml diff --git a/ch02/.idea/misc.xml b/ch02/.idea/misc.xml new file mode 100644 index 0000000..e208459 --- /dev/null +++ b/ch02/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch02/.idea/modules.xml b/ch02/.idea/modules.xml new file mode 100644 index 0000000..166c8df --- /dev/null +++ b/ch02/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ch02/ch02.iml b/ch02/ch02.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/ch02/ch02.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From cc24e3fa9a16cb739425fb99eb47837d795c46a2 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 15:28:46 -0500 Subject: [PATCH 05/22] Exercise 2-2(Think Java) --- ch02/Date.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ch02/Date.java diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..a09d982 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,21 @@ +public class Date +{ + public static void main (String [] Args) + { + int date, year; + String day; + String month; + + + date =17; + day = "Tuesday"; + month = "April "; + year= 2018; + + System.out.println("American Format:" ); + System.out.println( day +", " + month + date +", "+year); + + System.out.println("European Format:"); + System.out.println(day+" " + date +" "+ month +year); + } +} From 3508d14e12d3a83c4d5768f45c72d90a4e2ecbd4 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 17 Apr 2018 20:05:11 -0500 Subject: [PATCH 06/22] Exercise 2-A- 2-E --- ch02/DoubleByZero.java | 10 +++++++++ ch02/IntByZero.java | 11 ++++++++++ ch02/IntExtremes.java | 18 +++++++++++++++++ ch02/LoveJava.java | 12 +++++++++++ ch02/Time.java | 46 ++++++++++++++++++++++++++++++++++++++++++ ch02/Withdrawal.java | 25 +++++++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 ch02/DoubleByZero.java create mode 100644 ch02/IntByZero.java create mode 100644 ch02/IntExtremes.java create mode 100644 ch02/LoveJava.java create mode 100644 ch02/Time.java create mode 100644 ch02/Withdrawal.java diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..375990b --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,10 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double x=42; + double y=0; + double result=x/y; + System.out.println(result); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..011ece5 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,11 @@ +public class IntByZero +{ + public static void main(String[] arg) + { + int x=42; + int y=0; + int result; + result= x/y; + System.out.println(result); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..5b96e04 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,18 @@ +public class IntExtremes +{ + public static void main(String[] args) + { + int positiveInt= 2147483647; + System.out.println(positiveInt); + + positiveInt= positiveInt+1; + System.out.println(positiveInt); + + int negativeInt = -2147483648; + System.out.println(negativeInt); + + negativeInt= negativeInt-1; + System.out.println(negativeInt); + + } +} diff --git a/ch02/LoveJava.java b/ch02/LoveJava.java new file mode 100644 index 0000000..c4ac1f1 --- /dev/null +++ b/ch02/LoveJava.java @@ -0,0 +1,12 @@ +public class LoveJava +{ + public static void main(String[] args) + { + String a,b,c; + a="I "; + b="love "; + c="Java!"; + + System.out.println(a+b+c); + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..0d2089e --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,46 @@ +public class Time +{ + public static void main(String[] args) + { + double hour, minute, second; + String time; + String secondsLeft; + double midnight; + double secondsTotal; + double secondsInDay; + String percPassed; + double percent; + + time = "Number of seconds since Midnight: "; + secondsLeft= "The amount of seconds left in the day: "; + percPassed= "The percentage of the day that has passed: "; + + hour = 15.0; + minute = 32.0; + second = 45.0; + secondsInDay= (86400.0); + midnight= (second + (minute * 60) + (hour * 60 * 60)); + secondsTotal= (((23-hour)*60*60)+((59-minute)*60)+ (60-second)); + percent= ((midnight/secondsInDay)); + + System.out.println(time+midnight); + System.out.println(secondsLeft+secondsTotal); + System.out.println(percPassed+(percent*100)); + + hour = 16.0; + minute = 18.0; + second= 45.0; + + double timeNow; + String timeSince; + + timeNow= (second+(minute*60)+(hour*60*60)); + timeSince= "Seconds since exercise started: "; + + System.out.println(timeSince+(timeNow-midnight)); + + + + + } +} diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..5eb8908 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,25 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withdrawal; + withdrawal=137; + + int e=20; + int f=10; + int g=5; + int h=1; + int a= withdrawal/20; + int b= ((withdrawal-(a*e))/f); + int c= ((withdrawal-(a*e)-(b*f))/g); + int d= ((withdrawal-(a*e)-(b*f)-(c*g))/h); + + System.out.println("20s:"+a); + System.out.println("10s:"+b); + System.out.println("5s:"+c); + System.out.println("1s:"+d); + + System.out.println((a*e)+(b*f)+(c*g)+(d*h)); + + } +} From e89159bea699cd17be9c861588c25df989ca4b92 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 18 Apr 2018 18:58:18 -0500 Subject: [PATCH 07/22] Exercise 3-1 through 3-4 --- ch03/Exercise31.java | 9 +++++++++ ch03/GuessStarter.java | 16 +++++++++++++++- ch03/Temperature.java | 20 ++++++++++++++++++++ ch03/TotalTime.java | 29 +++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 ch03/Exercise31.java create mode 100644 ch03/Temperature.java create mode 100644 ch03/TotalTime.java diff --git a/ch03/Exercise31.java b/ch03/Exercise31.java new file mode 100644 index 0000000..f835c8b --- /dev/null +++ b/ch03/Exercise31.java @@ -0,0 +1,9 @@ +public class Exercise31 +{ + public static void main(String[] args) + { + double x=49; + System.out.printf("%d, %f",x); + + } +} diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 64984df..679ac3b 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,4 +1,5 @@ import java.util.Random; +import java.util.Scanner; /** * Starter code for the "Guess My Number" exercise. @@ -9,7 +10,20 @@ public static void main(String[] args) { // pick a random number Random random = new Random(); int number = random.nextInt(100) + 1; - System.out.println(number); + int guess; + Scanner in=new Scanner(System.in); + + System.out.println("I'm thinking of a number between 1 and 100"); + System.out.println("(including both). Can you guess what it is?"); + System.out.println("Type a number:"); + + guess = in.nextInt(); + int difference= number-guess; + + System.out.println("Your guess is: "+guess); + System.out.println("The number I was thinking of is: "+number); + System.out.println("You were off by: "+difference); + } } diff --git a/ch03/Temperature.java b/ch03/Temperature.java new file mode 100644 index 0000000..6bc0a39 --- /dev/null +++ b/ch03/Temperature.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class Temperature +{ + public static void main(String[] args) + { + double celsius; + double farenheit; + Scanner in = new Scanner (System.in); + + System.out.print("What is the temperature in Celsius?"); + celsius= in.nextDouble(); + System.out.println("It is "+celsius+" degrees Celsius."); + + farenheit= (celsius*9.0/5.0)+32.0; + System.out.println("The temperature in farenheit is "+farenheit); + + + } +} diff --git a/ch03/TotalTime.java b/ch03/TotalTime.java new file mode 100644 index 0000000..411c98c --- /dev/null +++ b/ch03/TotalTime.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class TotalTime +{ + public static void main (String[] args) + { + System.out.println("TotalTime"); + + Scanner in= new Scanner(System.in); + int seconds; + //seconds to minutes + int hours=3600; + int minutes=60; + //totals + int totalSeconds; + int totalMinutes; + int totalHours; + //how long did you run? + System.out.println("How many seconds did you run?"); + seconds=in.nextInt(); + System.out.println("You ran "+seconds+" seconds!"); + //number of hours,minutes, seconds + totalHours= seconds/hours; + totalMinutes= ((seconds-(hours*totalHours))/minutes); + totalSeconds= (seconds-(hours*totalHours))%minutes; + System.out.printf("%d seconds= %d hours,%d minutes, %d seconds",seconds,totalHours,totalMinutes,totalSeconds); + + } +} From bb8c7ad2ca8889cb03dde5dbce01c3e95ae45779 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 18 Apr 2018 19:15:17 -0500 Subject: [PATCH 08/22] Exercise 3-ES-1 --- ch03/Escape.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch03/Escape.java diff --git a/ch03/Escape.java b/ch03/Escape.java new file mode 100644 index 0000000..f5c7cd1 --- /dev/null +++ b/ch03/Escape.java @@ -0,0 +1,17 @@ +public class Escape +{ + public static void main(String[] args) + { + String header= "\n\tNEW YORK 3-DAY FORECAST:\n"; + header += "\n\tDay\t\tHigh\tLow\tConditions\n"; + header += "\t---\t\t----\t---\t----------\n"; + + String forecast= "\tSunday\t68F\t\t48F\tSunny\n"; + forecast += "\tMonday\t69F\t\t57F\tSunny\n"; + forecast += "\tTueday\t71F\t\t50F\tCloudy\n"; + + System.out.println(header+forecast); + + } + +} From e6e328444dfd0ff92d1ea969179f509c279716ef Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 18 Apr 2018 21:48:24 -0500 Subject: [PATCH 09/22] Chapter 4 Assignments --- ch04/.idea/vcs.xml | 6 ++++ ch04/BigMethodSignature.java | 12 ++++++++ ch04/DemoMath.java | 13 ++++++++ ch04/Employee.java | 57 ++++++++++++++++++++++++++++++++++++ ch04/MathUtil.java | 16 ++++++++++ ch04/SimpleMethods.java | 23 +++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 ch04/.idea/vcs.xml create mode 100644 ch04/BigMethodSignature.java create mode 100644 ch04/DemoMath.java create mode 100644 ch04/Employee.java create mode 100644 ch04/MathUtil.java create mode 100644 ch04/SimpleMethods.java diff --git a/ch04/.idea/vcs.xml b/ch04/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch04/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch04/BigMethodSignature.java b/ch04/BigMethodSignature.java new file mode 100644 index 0000000..1e05972 --- /dev/null +++ b/ch04/BigMethodSignature.java @@ -0,0 +1,12 @@ +public class BigMethodSignature +{ + public static void main(String[] args) + { + printSum(1,2,3,4,5,6,7,8,9,10); + printSum(10,20,30,40,50,60,70,80,90,100); + } + private static void printSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) + { + System.out.println(a+b+c+d+e+f+g+h+i+j); + } +} diff --git a/ch04/DemoMath.java b/ch04/DemoMath.java new file mode 100644 index 0000000..e51bb05 --- /dev/null +++ b/ch04/DemoMath.java @@ -0,0 +1,13 @@ +public class DemoMath +{ + public static void main(String[] args) + { + int a = -10; + int b = 20; + + System.out.println("The absolute value of -10 is: "+Math.abs(a)); + System.out.println("The max of -10 and 20 is: "+Math.max(a,b)); + System.out.println("-10 raised to the -10th power is: "+Math.pow(a,a)); + System.out.println("The value of PI is: "+Math.PI); + } +} diff --git a/ch04/Employee.java b/ch04/Employee.java new file mode 100644 index 0000000..6f44443 --- /dev/null +++ b/ch04/Employee.java @@ -0,0 +1,57 @@ +import java.util.Scanner; +import java.util.Random; + +public class Employee +{ + public static void main(String[] args) + { + System.out.println("Employee"); + int birthYear= 1995; + boolean isUnionMember= true; + int employeeNumber; + Scanner scanner=new Scanner(System.in); + + printHeader(); + System.out.println("Please enter your 5 digit employee number: "); + employeeNumber=scanner.nextInt(); + printFullName("Luke ","Allen ", "Martin, "); + printUnionStatus(true); + printAge(1995); + printEvenOrOdd(101545); + printGenerateSecretPassword(2); + + } + private static void printHeader() + { + System.out.println("Welcome to the WallabyTech Employee Application"); + System.out.println("==============================================="); + } + private static void printFullName (String first, String middle, String last) + { + System.out.println(last+first+middle); + } + private static void printUnionStatus(boolean union) + { + System.out.println("Your union status is: "+union); + } + private static void printAge(int birthYear) + { + int currentYear= 2018; + System.out.println("Your age is: "+(currentYear-birthYear)); + } + public static void printEvenOrOdd(int eo) + { + System.out.println("Employee number is even/odd (1=odd, 0=even);"); + System.out.println(eo%2); + } + public static void printGenerateSecretPassword(int password) + { + Random random=new Random(); + int employeeNumber=101545; + int randomNumber=random.nextInt(10)+1; + + System.out.println("Employee's random secret pw is: "+(employeeNumber*randomNumber)*5); + } +} + + diff --git a/ch04/MathUtil.java b/ch04/MathUtil.java new file mode 100644 index 0000000..125099f --- /dev/null +++ b/ch04/MathUtil.java @@ -0,0 +1,16 @@ +public class MathUtil +{ + public static void main(String[] args) + { + printDifference(1000,400000); + } + public static void printDifference(int x, int y) + { + printAbsValue(x-y); + } + public static void printAbsValue(int z) + { + System.out.println("Value is: "+z+"\nAbsolute Value is: "+Math.abs(z)); + } + +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..028973f --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,23 @@ +public class SimpleMethods +{ + public static void main(String[] args) + { + printCount(5); + printSum(4,6); + printSum(7,2); + printBoolean(true); + printBoolean(false); + } + private static void printCount(int count) + { + System.out.println("The count is: "+count); + } + private static void printSum(int x,int y) + { + System.out.println(x+y); + } + private static void printBoolean(boolean isStudent) + { + System.out.println("I am a student: "+isStudent); + } +} From d7a7e0708dea06e8475770eae8a98bac2570fa40 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 19 Apr 2018 09:56:15 -0500 Subject: [PATCH 10/22] Updated 4-2 --- ch04/SimpleMethods.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java index 028973f..056f217 100644 --- a/ch04/SimpleMethods.java +++ b/ch04/SimpleMethods.java @@ -14,6 +14,7 @@ private static void printCount(int count) } private static void printSum(int x,int y) { + System.out.print(x+ " + " +y+ " = "); System.out.println(x+y); } private static void printBoolean(boolean isStudent) From b8be3df420385b0612c6fe7ecc0e915d21fbd1d4 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 19 Apr 2018 09:59:07 -0500 Subject: [PATCH 11/22] Updated 4-H --- ch04/Employee.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch04/Employee.java b/ch04/Employee.java index 6f44443..c441152 100644 --- a/ch04/Employee.java +++ b/ch04/Employee.java @@ -39,12 +39,12 @@ private static void printAge(int birthYear) int currentYear= 2018; System.out.println("Your age is: "+(currentYear-birthYear)); } - public static void printEvenOrOdd(int eo) + private static void printEvenOrOdd(int eo) { System.out.println("Employee number is even/odd (1=odd, 0=even);"); System.out.println(eo%2); } - public static void printGenerateSecretPassword(int password) + private static void printGenerateSecretPassword(int password) { Random random=new Random(); int employeeNumber=101545; From 069f0e73f8d23eae096214916bfd7f4f351a0c87 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 19 Apr 2018 15:19:55 -0500 Subject: [PATCH 12/22] Exercise 5A-5G --- ch05/Comparison.java | 21 +++++++++++++++ ch05/Condition.java | 15 +++++++++++ ch05/Else.java | 16 +++++++++++ ch05/If.java | 15 +++++++++++ ch05/Logic.java | 18 +++++++++++++ ch05/LogicMethods.java | 52 +++++++++++++++++++++++++++++++++++ ch05/Precedence.java | 15 +++++++++++ ch05/StringCheese.java | 46 +++++++++++++++++++++++++++++++ ch05/Switch.java | 17 ++++++++++++ ch05/SwitchExample.java | 60 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 275 insertions(+) create mode 100644 ch05/Comparison.java create mode 100644 ch05/Condition.java create mode 100644 ch05/Else.java create mode 100644 ch05/If.java create mode 100644 ch05/Logic.java create mode 100644 ch05/LogicMethods.java create mode 100644 ch05/Precedence.java create mode 100644 ch05/StringCheese.java create mode 100644 ch05/Switch.java create mode 100644 ch05/SwitchExample.java diff --git a/ch05/Comparison.java b/ch05/Comparison.java new file mode 100644 index 0000000..a1ad53d --- /dev/null +++ b/ch05/Comparison.java @@ -0,0 +1,21 @@ +public class Comparison +{ + public static void main(String[] args) + { + String txt= "Fantastic"; + String lang= "Java"; + boolean state= (txt==lang); //assign test result + System.out.println("String Equality Test: "+state); + + state= (txt != lang); //assign result + System.out.println("String Inequality Test: "+state); + + int dozen=12; + int score=20; + state= (dozen>score); //assign result + System.out.println("Greater Than Test: "+state); + + state = (dozen1) System.out.println("Five is greater than one."); + if (2<4) { + System.out.println("Two is less than four."); + System.out.println("Test succeeded."); + } + int num= 4; + if(((num>5)&&(num<10))||(num==12)) System.out.println("Number is 6-9 inclusive, or 12"); + } +} diff --git a/ch05/Logic.java b/ch05/Logic.java new file mode 100644 index 0000000..47516cc --- /dev/null +++ b/ch05/Logic.java @@ -0,0 +1,18 @@ +public class Logic +{ + public static void main(String[] args) + { + boolean yes= true; + boolean no= false; + + System.out.println("Both YesYes True: " +(yes&&yes)); + System.out.println("Both YesNo True: "+ (yes&&no)); + + System.out.println("Either YesYes True: "+ (yes||yes)); + System.out.println("Either YesNo True: "+(yes||no)); + System.out.println("Either NoNo True: "+(no||no)); + + System.out.println("Original Yes Value: "+yes); + System.out.println("Inverse Yes Value: "+!yes); + } +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..b7ab727 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,52 @@ +public class LogicMethods +{ + public static void main(String [] args) + { + printIsLarge(100); + printIsLarge(20); + printIsLarge(1000); + printIsLargeOrSmall(5); + printIsLargeOrSmall(100); + printIsLargeOrSmall(1); + printLargest(10,2); + printLargest(4,8); + printLargest(5,5); + printLargestOdd(27,10); + printLargestOdd(13,15); + printLargestOdd(10,4); + } + private static void printIsLarge(int number) + { + if (number>99) + System.out.println("The number is large"); + } + private static void printIsLargeOrSmall(int number) + { + if (number>99) + System.out.println("The number is large"); + if (number<10) + System.out.println("The number is small"); + } + private static void printLargest(int number1,int number2) + { + if (number1>number2) + System.out.println("The largest number is: "+number1); + else if (number2>number1) + System.out.println("The largest number is: "+number2); + else + System.out.println("The numbers are equal"); + } + private static void printLargestOdd(int number1,int number2) + { + if ((number1%2!=0&&(number1>number2))) + System.out.println("The largest odd number is: "+number1); + else if ((number2%2!=0&&(number2>number1))) + System.out.println("The largest odd number is: "+number2); + else + { + System.out.print("Two odds make an even "); + System.out.print(number1 + " + " + number2); + System.out.println(" = "+(number1+number2)); + } + } +} diff --git a/ch05/Precedence.java b/ch05/Precedence.java new file mode 100644 index 0000000..931ad8c --- /dev/null +++ b/ch05/Precedence.java @@ -0,0 +1,15 @@ +public class Precedence +{ + public static void main(String[] args) + { + int sum= 32-8+16*2; //16x2=32, +24=56 + System.out.println("Default order: "+sum); + + sum =(32-8+16)*2; //24+16=40, x 2= 80 + System.out.println("Specified order: "+sum); + + sum= (32-(8+16))*2; //32-24=8, *2= 16 + System.out.println("Nested specific order: "+sum); + + } +} diff --git a/ch05/StringCheese.java b/ch05/StringCheese.java new file mode 100644 index 0000000..c69c867 --- /dev/null +++ b/ch05/StringCheese.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class StringCheese +{ + public static void main(String[] args) + { + /* + 1&2 inch cheese= $2 shipping per yard + 3 inch cheese= $4 shipping per yard + 1 inch= $2/yard + 2 inch= $4/yard + 3 inch= $6/yard + "The cost of your cheese is $"+ + */ + int inchPrice= 2; + int inch2Price=4; + int inch3Price=6; + int shipping12=2; + int shipping3=4; + int yards; //yards ordered + int cheese; //type of cheese + + Scanner amount= new Scanner(System.in); //get yards + Scanner type= new Scanner (System.in); //get cheese type + + System.out.println("What diameter cheese would you like to order?"); + cheese=type.nextInt(); + System.out.println("How many yards would you like to order?"); + yards=amount.nextInt(); + + if ((yards>50&&cheese==1)) + System.out.println("The cost of your cheese is $"+((yards*inchPrice)+5)); + else if ((yards<=50&&cheese==1)) + System.out.println("The cost of your cheese is $"+((yards*inchPrice)+(yards*shipping12)+5)); + else if ((yards>75&&cheese==2)) + System.out.println("The cost of your cheese is $"+((yards*inch2Price)+5)); + else if ((yards<=75&&cheese==2)) + System.out.println("The cost of your cheese is $"+((yards*inch2Price)+(yards*shipping12)+5)); + else if ((yards>25&&cheese==3)) + System.out.println("The cost of your cheese is $"+((yards*inch3Price)+5)); + else if ((yards<=25&&cheese==3)) + System.out.println("The cost of your cheese is $"+((yards*inch3Price)+(yards*shipping3)+5)); + else + System.out.println("This order is too crazy!"); + } +} diff --git a/ch05/Switch.java b/ch05/Switch.java new file mode 100644 index 0000000..b6445cf --- /dev/null +++ b/ch05/Switch.java @@ -0,0 +1,17 @@ +public class Switch +{ + public static void main (String[] args) + { + int month= 2; + int year = 2018; + int num =31; + + switch(month){ + case 4: case 6: case 9: case 11: + num=30; break; + case 2: + num=((year%4)==0)? 29:28; break; + } + System.out.println(month+"/"+year+": "+num+" days"); + } +} diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java new file mode 100644 index 0000000..b9c3a27 --- /dev/null +++ b/ch05/SwitchExample.java @@ -0,0 +1,60 @@ +public class SwitchExample +{ + public static void main(String[] args) + { + lastNameWinner("smith"); + lastNameWinner("jones"); + lastNameWinner("Lazenby"); + lastNameWinner("Martin"); + dayOfWeek(2); + dayOfWeek(4); + dayOfWeek(-2); + dayOfWeek(100); + + } + public static void lastNameWinner(String winner) + { + switch (winner) + { + case "smith": + case "jones": + System.out.println("Congratulations, grand winner"); + break; + case "Lazenby": + System.out.println("Hey, he owes me dinner"); + break; + default: + System.out.println("Sorry, not a winner"); + } + } + private static void dayOfWeek(int day) + { + switch (day) + { + case 1: + System.out.println("Sunday"); + break; + case 2: + System.out.println("Monday"); + break; + case 3: + System.out.println("Tuesday"); + break; + case 4: + System.out.println("Wednesday"); + break; + case 5: + System.out.println("Thursday"); + break; + case 6: + System.out.println("Friday"); + break; + case 7: + System.out.println("Saturday"); + break; + default: + System.out.println("Invalid value: day"); + } + + } +} From 959fecb8c91a9176531ec76271d2b70ece1a1dd2 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 19 Apr 2018 15:34:13 -0500 Subject: [PATCH 13/22] Exercise 5H --- ch05/TicketNumber.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ch05/TicketNumber.java diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..f857f26 --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class TicketNumber +{ + public static void main (String [] args) + { + int ticketNumber; + Scanner myTicket=new Scanner(System.in); + System.out.println("What is your ticket number?"); + ticketNumber=myTicket.nextInt(); + + int lastDigit=ticketNumber%10; + int ticketPrefix= (ticketNumber-lastDigit); + if ((ticketPrefix/10)%7==lastDigit) + System.out.println("Good Number"); + else + System.out.println("Bad Number"); + + } +} From ded09902edc679d1228da6826f95c6c0571c5d8a Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 19 Apr 2018 20:13:00 -0500 Subject: [PATCH 14/22] Exercise 5H --- ch05/Coffee.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ch05/Coffee.java diff --git a/ch05/Coffee.java b/ch05/Coffee.java new file mode 100644 index 0000000..bb6d1fb --- /dev/null +++ b/ch05/Coffee.java @@ -0,0 +1,40 @@ +public class Coffee +{ + public static void main(String[] args) + { + /* + Travel<21=free coffee + Bike<30=10% + Ride>=35 =20% + Bike <50 = 20% + Ride <50 = 30% + */ + bikeDiscount(20); + bikeDiscount(30); + bikeDiscount(52); + busDiscount(35); + busDiscount(50); + } + public static void bikeDiscount(int milesRidden) + { + int discount=0; + if (milesRidden<21) + discount=100; + if (milesRidden<50&&milesRidden>=30) + discount=20; + if (milesRidden<30&&milesRidden>=21) + discount=30; + System.out.println("You receive "+discount+"% off!"); + } + public static void busDiscount(int milesRidden) + { + int discount=0; + if (milesRidden<21) + discount=100; + if (milesRidden>=35&&milesRidden<50) + discount=50; + if (milesRidden<=50&&milesRidden>35) + discount=30; + System.out.println("You receive "+discount+"% off!"); + } +} From e464a2b6a30aa137cdd0624f598a6677e77c36ac Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 22 Apr 2018 19:11:30 -0500 Subject: [PATCH 15/22] ex 6-2 through 6-B --- ch06/Divisible.java | 36 ++++++++++++++++++++++++++++++++++++ ch06/Exercise.java | 1 + ch06/MathUtil.java | 28 ++++++++++++++++++++++++++++ ch06/Multadd.java | 13 +++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 ch06/Divisible.java create mode 100644 ch06/MathUtil.java create mode 100644 ch06/Multadd.java diff --git a/ch06/Divisible.java b/ch06/Divisible.java new file mode 100644 index 0000000..60a1d27 --- /dev/null +++ b/ch06/Divisible.java @@ -0,0 +1,36 @@ +public class Divisible +{ + public static void main(String [] args) + { + System.out.println(isDivisible(10,5)); + System.out.println(isDivisible(10,3)); + System.out.println(isDivisible(12,5)); + System.out.println(isDivisible(25,5)); + System.out.println(isTriangle(10,2,5)); + System.out.println(isTriangle(10,7,4)); + + } + private static boolean isDivisible(int n, int m) + { + boolean answer=false; + int div=n%m; + if (div==0) + { + answer=true; + } + return answer; + } + + private static boolean isTriangle(int a,int b, int c) + { + boolean answer= false; + + if(a<(b+c)&&b<(a+c)&&c<(b+a)) + { + answer=true; + } + return answer; + } + + +} diff --git a/ch06/Exercise.java b/ch06/Exercise.java index b826b44..7cf45db 100644 --- a/ch06/Exercise.java +++ b/ch06/Exercise.java @@ -1,6 +1,7 @@ public class Exercise { public static void main(String[] args) { + boolean flag1 = isHoopy(202); boolean flag2 = isFrabjuous(202); System.out.println(flag1); diff --git a/ch06/MathUtil.java b/ch06/MathUtil.java new file mode 100644 index 0000000..7f18605 --- /dev/null +++ b/ch06/MathUtil.java @@ -0,0 +1,28 @@ +public class MathUtil +{ + public static void main (String [] args) + { + System.out.println(absoluteSum(10,-20)); + System.out.println(absoluteSum(0,-1)); + System.out.println(absoluteSum(12,12)); + System.out.println(absoluteOverload(5,-20,-80)); + System.out.println(absoluteOverload(-10,10,-1)); + System.out.println(absoluteOverload(2,3,4)); + + } + private static int absoluteSum(int x, int y) + { + int absX= Math.abs(x); + int absY= Math.abs(y); + + return (absX+absY); + } + private static int absoluteOverload(int x, int y, int z) + { + int absX= Math.abs(x); + int absY= Math.abs(y); + int absZ= Math.abs(z); + + return (absX+absY+absZ); + } +} diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..7d2123c --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,13 @@ +public class Multadd +{ + public static void main(String[] args) + { + System.out.println(multadd(1,2,3)); + + } + public static double multadd(double a, double b, double c) + { + double answer = a * b + c; + return answer; + } +} From f6e777b5e0aa9fa89821676a4f77d220d516ecc0 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 23 Apr 2018 15:09:37 -0500 Subject: [PATCH 16/22] Exercise 7A-7F --- ch07/DoWhile.java | 14 ++++++++++++++ ch07/For.java | 17 +++++++++++++++++ ch07/LoopTest.java | 23 +++++++++++++++++++++++ ch07/LoopTestB.java | 23 +++++++++++++++++++++++ ch07/LoopTestC.java | 23 +++++++++++++++++++++++ ch07/LoopTestD.java | 17 +++++++++++++++++ ch07/LoopTestE.java | 23 +++++++++++++++++++++++ ch07/LoopTestF.java | 25 +++++++++++++++++++++++++ ch07/While.java | 13 +++++++++++++ 9 files changed, 178 insertions(+) create mode 100644 ch07/DoWhile.java create mode 100644 ch07/For.java create mode 100644 ch07/LoopTest.java create mode 100644 ch07/LoopTestB.java create mode 100644 ch07/LoopTestC.java create mode 100644 ch07/LoopTestD.java create mode 100644 ch07/LoopTestE.java create mode 100644 ch07/LoopTestF.java create mode 100644 ch07/While.java diff --git a/ch07/DoWhile.java b/ch07/DoWhile.java new file mode 100644 index 0000000..ca20cca --- /dev/null +++ b/ch07/DoWhile.java @@ -0,0 +1,14 @@ +public class DoWhile +{ + public static void main(String[] args) + { + int num= 100; + + do + { + System.out.println("DoWhile Countup: " +num); + num+=10; + } + while(num<10); + } +} diff --git a/ch07/For.java b/ch07/For.java new file mode 100644 index 0000000..65e2d7c --- /dev/null +++ b/ch07/For.java @@ -0,0 +1,17 @@ +public class For +{ + public static void main (String [] args) + { + int num=0; + + for (int i=1; i<4;i++) + { + System.out.println("Outer Loop i= "+i); + for(int j=1; j<4;j++) + { + System.out.print("\tInner Loop j="+j); + System.out.println("\t\tTotal num="+(++num)); + } + } + } +} diff --git a/ch07/LoopTest.java b/ch07/LoopTest.java new file mode 100644 index 0000000..c08d0ad --- /dev/null +++ b/ch07/LoopTest.java @@ -0,0 +1,23 @@ +public class LoopTest +{ + public static void main(String [] args) + { + int y=1; + int z=1; + for (int x=1; x<=10; x++) + { + System.out.println("The count of x is: " +x); + } + while (y<=10) + { + System.out.println("The count of y is: "+y); + y++; + } + do + { + System.out.println("The count of z is : "+z); + z++; + } + while(z<=10); + } +} diff --git a/ch07/LoopTestB.java b/ch07/LoopTestB.java new file mode 100644 index 0000000..e37c990 --- /dev/null +++ b/ch07/LoopTestB.java @@ -0,0 +1,23 @@ +public class LoopTestB +{ + public static void main (String [] args) + { + for(int x=0; x<=100;x+=10) + { + System.out.println("The count of x is: "+x); + } + int y=0; + while (y<=100) + { + System.out.println("The count of y is: "+y); + y+=10; + } + int z=0; + do + { + System.out.println("The count of z is: "+z); + z+=10; + } + while (z<=100); + } +} diff --git a/ch07/LoopTestC.java b/ch07/LoopTestC.java new file mode 100644 index 0000000..c205037 --- /dev/null +++ b/ch07/LoopTestC.java @@ -0,0 +1,23 @@ +public class LoopTestC +{ + public static void main (String [] args) + { + for (int x=100; x>=-100; x-=8) + { + System.out.println("The count of x is: "+x); + } + int y=100; + while(y>=-100) + { + System.out.println("The count of y is: "+y); + y-=8; + } + int z=100; + do + { + System.out.println("The count of z is: "+z); + z-=8; + } + while(z>=-100); + } +} diff --git a/ch07/LoopTestD.java b/ch07/LoopTestD.java new file mode 100644 index 0000000..224cee8 --- /dev/null +++ b/ch07/LoopTestD.java @@ -0,0 +1,17 @@ +public class LoopTestD +{ + public static void main (String [] args) + { + numSequence(50); + } + + private static int numSequence(int x) + { + int num=1; + while(num<=x) + { + System.out.println(num++); + } + return num; + } +} diff --git a/ch07/LoopTestE.java b/ch07/LoopTestE.java new file mode 100644 index 0000000..c4b7781 --- /dev/null +++ b/ch07/LoopTestE.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class LoopTestE +{ + public static void main(String[] args) + { + zero(); + System.out.println("The number is zero"); + } + + private static int zero() + { + Scanner in = new Scanner(System.in); + System.out.println("Please type the number zero"); + int x = in.nextInt(); + while (x != 0) + { + System.out.println("Please type the number zero"); + x= in.nextInt(); + } + return x; + } +} \ No newline at end of file diff --git a/ch07/LoopTestF.java b/ch07/LoopTestF.java new file mode 100644 index 0000000..8f13285 --- /dev/null +++ b/ch07/LoopTestF.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +public class LoopTestF +{ + public static void main (String [] args) + { + runningTotal(); + } + private static int runningTotal() + { + Scanner in= new Scanner(System.in); + System.out.println("Enter a number: "); + int x= in.nextInt(); + int sum=0; + while(sum<=1000) + { + sum+=x; + System.out.println("The total of the numbers is " + sum); + System.out.println("Please enter another number: "); + x=in.nextInt(); + } + System.out.println("The final total is: "+sum); + return x; + } +} diff --git a/ch07/While.java b/ch07/While.java new file mode 100644 index 0000000..a21cecc --- /dev/null +++ b/ch07/While.java @@ -0,0 +1,13 @@ +public class While +{ + public static void main (String [] args) + { + int num=100; + + while (num>0) + { + System.out.println("While Countdown: " + num); + num-=10; + } + } +} From 9a605ad1c6155deab92651ecd9e025c6613540c8 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 23 Apr 2018 15:17:29 -0500 Subject: [PATCH 17/22] Exercise 7A-7F --- ch07/LoopTestF.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch07/LoopTestF.java b/ch07/LoopTestF.java index 8f13285..db85c87 100644 --- a/ch07/LoopTestF.java +++ b/ch07/LoopTestF.java @@ -20,6 +20,6 @@ private static int runningTotal() x=in.nextInt(); } System.out.println("The final total is: "+sum); - return x; + return sum; } } From 2e7e9564229f664deb083ff2f4576101e5822778 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 23 Apr 2018 17:17:27 -0500 Subject: [PATCH 18/22] Exercise 7A-7F --- ch07/MultiplicationTable.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ch07/MultiplicationTable.java diff --git a/ch07/MultiplicationTable.java b/ch07/MultiplicationTable.java new file mode 100644 index 0000000..716313d --- /dev/null +++ b/ch07/MultiplicationTable.java @@ -0,0 +1,27 @@ +public class MultiplicationTable +{ + public static void main (String [] args) + { + printTable(20); + } + public static void printRow(int n,int cols) + { + int x= 1; + + while (x<=cols) + { + System.out.printf("%4d",x*n); + x=x+1; + } + System.out.println(); + } + private static void printTable(int rows) + { + int y = 1; + while (y <= rows) + { + printRow(y,rows); + y=y+1; + } + } +} From 356ab499ee88ccd2aa0ca514fd2ff00600577794 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 24 Apr 2018 15:00:14 -0500 Subject: [PATCH 19/22] Ex 8-A through 8-G --- ch08/Array.java | 17 ++++++ ch08/ArrayDemo.java | 122 ++++++++++++++++++++++++++++++++++++++++++ ch08/HowManyDogs.java | 30 +++++++++++ 3 files changed, 169 insertions(+) create mode 100644 ch08/Array.java create mode 100644 ch08/ArrayDemo.java create mode 100644 ch08/HowManyDogs.java diff --git a/ch08/Array.java b/ch08/Array.java new file mode 100644 index 0000000..67bde9a --- /dev/null +++ b/ch08/Array.java @@ -0,0 +1,17 @@ +public class Array +{ + public static void main (String [] args) + { + String [] str={"Much ", "More ", "Java "}; + int [] num= new int[3]; + num[0]=100; + num[1]=200; + + str [1]= "Better "; + + System.out.println("String array length is "+str.length); + System.out.println("Integer array length is "+num.length); + System.out.println(num[0]+","+num[1]+","+num[2]); + System.out.println(str[0]+str[1]+str[2]); + } +} diff --git a/ch08/ArrayDemo.java b/ch08/ArrayDemo.java new file mode 100644 index 0000000..cf5ad5c --- /dev/null +++ b/ch08/ArrayDemo.java @@ -0,0 +1,122 @@ +public class ArrayDemo +{ + public static void main (String [] args) + { + + int [] numbers={1,5,9}; + printArray(numbers); + System.out.println(); + + int [] numbers2={5,7,13}; + int sum = arrayTotal(numbers2); + int sum2= arrayTotal(numbers); + printArray(numbers2); + System.out.println("The total of these numbers is: "+sum); + System.out.println(); + + printArray(numbers); + System.out.println("The total of these numbers is: "+sum2); + System.out.println(); + + int [] numbers3= {5,8,21,19,2}; + int max=(arrayMax(numbers3)); + printArray(numbers3); + System.out.println("The max of these numbers is: "+max); + System.out.println(); + + int [] numbers4= {78,23,9,34}; + int max2=(arrayMax(numbers4)); + printArray(numbers4); + System.out.println("The max of these numbers is: "+max2); + + int maxIndex=(arrayMaxIndex(numbers4)); + System.out.println(maxIndex); + int maxIndex2= arrayMaxIndex(numbers3); + System.out.println(maxIndex2); + System.out.println(); + + double [] numbers5= {34.2,18.0,12.5,13.1}; + double avg= arrayAverage(numbers5); + System.out.println("The average of these numbers is "+avg); + System.out.println(); + + double [] numbers6= {10.0,15.0,20.0}; + double avg2=(arrayAverage(numbers6)); + System.out.println("The average of these numbers is " +avg2); + + int [] keyword=new int [10]; + keyword [0]=4; + keyword [3]=2; + keyword [9]=4; + printArray(keyword); + + String [] keyword2= new String [10]; + keyword2 [0]= "Hi"; + keyword2 [3]= "Hello"; + keyword2 [9]= "Bye"; + printStrings(keyword2); + + + + } + private static void printArray(int [] values) + { + for (int value:values) + { + System.out.println(value); + } + } + private static int arrayTotal(int[] values) + { + int sum=0; + for(int value: values) + { + sum=sum+value; + } + return sum; + } + + private static int arrayMax(int [] values) + { + int max=0; + for(int value:values) + { + if(value>max) + max = value; + } + return max; + } + private static int arrayMaxIndex( int [] values) + { + int max=0; + int maxIndex=0; + + for (int x = 0; x < values.length; x++) + { + if(values[x]>max) + { + max=values[x]; + maxIndex=x; + } + } + return maxIndex; + } + private static double arrayAverage(double []values) + { + double sum=0; + double length= values.length; + for (double value:values) + { + sum= sum+value; + } + double avg= sum/length; + return avg; + } + private static void printStrings (String [] keywords) + { + for (String stringTest:keywords) + { + System.out.println(stringTest); + } + } +} diff --git a/ch08/HowManyDogs.java b/ch08/HowManyDogs.java new file mode 100644 index 0000000..a8aa523 --- /dev/null +++ b/ch08/HowManyDogs.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +public class HowManyDogs +{ + public static void main (String [] args) + { + int numDogs=0; + + System.out.println("How many dogs do you have?"); + Scanner dogs= new Scanner(System.in); + numDogs=dogs.nextInt(); + + String [] dogName= new String[numDogs]; + System.out.println("What is your dogs name?"); + Scanner name= new Scanner(System.in); + for (int number=0;number Date: Tue, 24 Apr 2018 16:13:35 -0500 Subject: [PATCH 20/22] VendingMachine progress --- ch08/VendingMachine.java | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ch08/VendingMachine.java diff --git a/ch08/VendingMachine.java b/ch08/VendingMachine.java new file mode 100644 index 0000000..0df1665 --- /dev/null +++ b/ch08/VendingMachine.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +public class VendingMachine +{ + public static void main(String[] args) + { + String[] meals = new String[3]; + meals[0] = "0)\tFreeze Dried Sushi"; + meals[1] = "1)\tSpock's Brain Blast"; + meals[2] = "2)\tAlien Asparagus"; + printMenu(meals); + System.out.println("Your selection: "); + int[] numSold = new int[3]; + printSales(numSold); + } + + private static void printMenu(String[] menu) + { + System.out.println("Please select from: "); + for (String menuOptions : menu) + { + System.out.println(menuOptions); + } + } + + private static void printSales(int[] sales) + { + for (int totalSales : sales) + { + Scanner choose = new Scanner(System.in); + int choice = choose.nextInt(); + int sushi = 0; + int spock = 0; + int asparagus = 0; + + if (choice == 99) + System.out.println("Goodbye!"); + else if (choice == 0) + { + for (sushi = 0; sushi < 99; sushi++) ; + { + sushi+=totalSales; + } + } else if (choice == 1) + { + for (totalSales = 0; totalSales < 99; totalSales++) ; + { + spock = totalSales + 1; + } + } else if (choice == 2) + { + for (totalSales = 0; totalSales < 99; totalSales++) ; + { + asparagus = totalSales + 1; + } + } + System.out.println("Thank you for choosing Alien Asparagus"); + System.out.println("Sold so far:"); + System.out.println(sushi + " of Freeze Dried Sushi"); + System.out.println(spock + " of Spock's Brain Blast"); + System.out.println(asparagus + " of Alien Asparagus"); + } + } +} \ No newline at end of file From c1dbad956751a19f63c46ce8e6be9733a82c3792 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 25 Apr 2018 10:25:15 -0500 Subject: [PATCH 21/22] VendingMachine progress --- ch08/VendingMachine.java | 88 ++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/ch08/VendingMachine.java b/ch08/VendingMachine.java index 0df1665..048b927 100644 --- a/ch08/VendingMachine.java +++ b/ch08/VendingMachine.java @@ -11,7 +11,53 @@ public static void main(String[] args) printMenu(meals); System.out.println("Your selection: "); int[] numSold = new int[3]; - printSales(numSold); + Scanner choose = new Scanner(System.in); + int sushi=0; + int spock=0; + int asparagus=0; + for (int x = 0; x >=0; x++) + { + + int choice = choose.nextInt(); + if (choice == 99) + { + break; + } + else if (choice == 0) + { + numSold[0] = numSold[0] + 1; + sushi = numSold[0]; + System.out.println("Thank you for choosing Alien Asparagus"); + System.out.println("Sold so far: "); + System.out.println(sushi + " Freeze Dried Sushi"); + System.out.println(spock + " Spock's Brain Blast"); + System.out.println(asparagus + " Alien Asparagus"); + } + + else if (choice == 1) + { + numSold[1] = numSold[1] + 1; + spock=numSold[1]; + System.out.println("Thank you for choosing Alien Asparagus"); + System.out.println("Sold so far: "); + System.out.println(sushi+" Freeze Dried Sushi"); + System.out.println(spock+" Spock's Brain Blast"); + System.out.println(asparagus+" Alien Asparagus"); + } + + else if (choice == 2) + { + numSold[2] = numSold[2] + 1; + asparagus=numSold[2]; + System.out.println("Thank you for choosing Alien Asparagus"); + System.out.println("Sold so far: "); + System.out.println(sushi+" Freeze Dried Sushi"); + System.out.println(spock+" Spock's Brain Blast"); + System.out.println(asparagus+" Alien Asparagus"); + } + } + System.out.println("Goodbye!"); + printSales(sushi,spock,asparagus); } private static void printMenu(String[] menu) @@ -23,42 +69,14 @@ private static void printMenu(String[] menu) } } - private static void printSales(int[] sales) + private static void printSales(int sushiPass, int spockPass, int asparagusPass) { - for (int totalSales : sales) { - Scanner choose = new Scanner(System.in); - int choice = choose.nextInt(); - int sushi = 0; - int spock = 0; - int asparagus = 0; - - if (choice == 99) - System.out.println("Goodbye!"); - else if (choice == 0) - { - for (sushi = 0; sushi < 99; sushi++) ; - { - sushi+=totalSales; - } - } else if (choice == 1) - { - for (totalSales = 0; totalSales < 99; totalSales++) ; - { - spock = totalSales + 1; - } - } else if (choice == 2) - { - for (totalSales = 0; totalSales < 99; totalSales++) ; - { - asparagus = totalSales + 1; - } - } System.out.println("Thank you for choosing Alien Asparagus"); - System.out.println("Sold so far:"); - System.out.println(sushi + " of Freeze Dried Sushi"); - System.out.println(spock + " of Spock's Brain Blast"); - System.out.println(asparagus + " of Alien Asparagus"); + System.out.println("Sold so far: "); + System.out.println(sushiPass + " Freeze Dried Sushi"); + System.out.println(spockPass + " Spock's Brain Blast"); + System.out.println(asparagusPass + " Alien Asparagus"); } } -} \ No newline at end of file +} From 72d8dbc37653bf1cc3e28c81575830da5d49fa60 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 25 Apr 2018 13:49:23 -0500 Subject: [PATCH 22/22] Ex9-B-1 through 9-B-6 --- ch09/StringUtil.java | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ch09/StringUtil.java diff --git a/ch09/StringUtil.java b/ch09/StringUtil.java new file mode 100644 index 0000000..4c09db2 --- /dev/null +++ b/ch09/StringUtil.java @@ -0,0 +1,61 @@ +public class StringUtil +{ + public static void main (String [] args) + { + getFirstCharacter("Hello"); + getFirstCharacter("Goodbye"); + getLastCharacter("Hello"); + getLastCharacter("Goodbye"); + getFirstTwoCharacters("Hello"); + getFirstTwoCharacters("Goodbye"); + getLastTwoCharacters("Hello"); + getLastTwoCharacters("Goodbye"); + getAllButFirstThreeCharacters("Hello"); + getAllButFirstThreeCharacters("Goodbye"); + printCharacters("Hello"); + + } + private static String getFirstCharacter(String test) + { + String answer= test.substring(0,1); + System.out.println(answer); + return answer; + } + private static String getLastCharacter (String test) + { + int start= test.length(); + String answer=test.substring(start-1); + System.out.println(answer); + return answer; + } + private static String getFirstTwoCharacters (String test) + { + String answer=test.substring(0,2); + System.out.println(answer); + return answer; + } + private static String getLastTwoCharacters (String test) + { + int start= test.length(); + String answer=test.substring(start-2); + System.out.println(answer); + return answer; + } + private static String getAllButFirstThreeCharacters(String test) + { + String answer=test.substring(3); + System.out.println(answer); + return answer; + } + private static int printCharacters(String test) + { + int position=0; + for(int x=0; x