Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ex13(){
- myWindow.clearOut();
- boolean b1 = true;
- boolean b2 = false;
- boolean b3; //new variable to put 1 value to be able to swap
- myWindow.writeOutLine("The value of b1 is: " + b1);
- myWindow.writeOutLine("The value fo b2 is: " + b2);
- //swap the values in 2 variables
- b3 = b1;
- b1 = b2;
- b2 = b3;
- myWindow.writeOutLine("The current state of b1 after reversal is: " + b1);
- myWindow.writeOutLine("The current state of b1 after reversal is: " + b2);
- }
- private void ex14(){
- myWindow.clearOut();
- //EQN = ax2 + bx + c = 0. Finding roots (x's) - declare variables
- double a = 2;
- double b = 7;
- double c = 1;
- double discriminant = Math.sqrt((Math.pow(b, 2))-(4*a*c));
- double numerator1 = -b + discriminant; //eq 1
- double numerator2 = -b - discriminant; //eq 2
- double denominator = 2*a;
- double root1 = numerator1/denominator; //eq to get x1
- double root2 = numerator2/denominator; //eq to get x2
- myWindow.writeOutLine("The root x1 is: " + root1);
- myWindow.writeOut("The root x2 is: " + root2);
- }
- private void ex22(){
- myWindow.clearOut();
- int n = 24;
- /* //TEST VALUES
- int n = 39;
- int n = 1822;
- int n = -12;
- int n = 0;
- */
- boolean remainder = ((n % 2) == 0);
- myWindow.writeOut("Is this number divisible by 2? " + n + ": " + remainder);
- }
- private void ex23(){
- myWindow.clearOut();
- int n = -83;
- /* //TEST VALUES
- int n = 39;
- int n = 1822;
- int n = -12;
- int n = 2;
- int n = -47;
- int n = 0;
- */
- boolean negNum = n < 0;
- boolean oddNum = (n % 2 == 1); /
- boolean negAndOdd = negNum && oddNum;
- myWindow.writeOut("Is this number negative, and odd? " + n + ": " + negAndOdd);
- }
- private void ex24(){
- myWindow.clearOut();
- int n = 22;
- /* //TEST VALUES
- int n = 42;
- int n = 39;
- int n = 1822;
- int n = -12;
- int n = 2;
- int n = -47
- int n = 0;
- */
- boolean x1;
- boolean x2;
- boolean x3;
- boolean result;
- x1 = (n >= 1 && n <= 100); //1 to 100 inclusive
- x2 = (n >= 40 && n <= 50); //40 to 50 inclusive
- x3 = (n % 2 == 0); //even numbers
- result = x1 && !(x2 && x3);
- myWindow.writeOutLine(result);
- }
- private void ex25(){
- myWindow.clearIn();
- myWindow.clearOut();
- String userInput;
- userInput = myWindow.readIn();
- boolean userResult = userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("n");
- myWindow.writeOut(userResult);
- }
- private void ex26(){
- myWindow.clearOut();
- String userInput;
- userInput = "1998done"; //returns true if a String variable “userInput” does not have the value “done”, false if has done
- // userInput = "done"; //prints false
- // userInput = "Done"; //prints true - capital D used
- // userInput = "Jasmine Kaur"; //prints true
- boolean userResult = !userInput.contains("done");
- myWindow.writeOut(userResult);
- }
- private void ex28(){
- myWindow.clearOut();
- //set of test values
- int year = 2016;
- //int year = 2017;
- //int year = 4929;
- //int year = 2028;
- boolean x1 = year % 4 == 0;
- boolean x2 = year % 100 != 0;
- boolean x3 = year % 400 == 0;
- boolean IsLeapYear;
- IsLeapYear = x1 && x2 || x3;
- myWindow.writeOutLine("Is " + year + " a leap year? " + IsLeapYear);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement