Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. private void ex13(){
  2.         myWindow.clearOut();
  3.  
  4.         boolean b1 = true;
  5.         boolean b2 = false;
  6.         boolean b3;             //new variable to put 1 value to be able to swap
  7.  
  8.         myWindow.writeOutLine("The value of b1 is: " + b1);
  9.         myWindow.writeOutLine("The value fo b2 is: " + b2);
  10.  
  11.         //swap the values in 2 variables
  12.         b3 = b1;                
  13.         b1 = b2;                
  14.         b2 = b3;              
  15.  
  16.         myWindow.writeOutLine("The current state of b1 after reversal is: " + b1);
  17.         myWindow.writeOutLine("The current state of b1 after reversal is: " + b2);
  18.  
  19.  
  20.  
  21.     }
  22.  
  23.     private void ex14(){
  24.         myWindow.clearOut();
  25.  
  26.         //EQN = ax2 + bx + c = 0. Finding roots (x's) - declare variables
  27.         double a = 2;
  28.         double b = 7;
  29.         double c = 1;
  30.  
  31.  
  32.         double discriminant = Math.sqrt((Math.pow(b, 2))-(4*a*c));          
  33.         double numerator1 = -b + discriminant;                    //eq 1
  34.         double numerator2 = -b - discriminant;                    //eq 2
  35.         double denominator = 2*a;
  36.  
  37.         double root1 = numerator1/denominator;                    //eq to get x1
  38.         double root2 = numerator2/denominator;                    //eq to get x2
  39.  
  40.         myWindow.writeOutLine("The root x1 is: " + root1);
  41.         myWindow.writeOut("The root x2 is: " + root2);
  42.  
  43.     }
  44.  
  45.     private void ex22(){
  46.         myWindow.clearOut();
  47.         int n = 24;
  48.  
  49.         /* //TEST VALUES
  50.         int n = 39;
  51.         int n = 1822;
  52.         int n = -12;
  53.         int n = 0;
  54.          */
  55.  
  56.         boolean remainder = ((n % 2) == 0);
  57.         myWindow.writeOut("Is this number divisible by 2? " + n + ": " + remainder);
  58.  
  59.     }
  60.  
  61.     private void ex23(){
  62.         myWindow.clearOut();
  63.         int n = -83;
  64.  
  65.         /* //TEST VALUES
  66.         int n = 39;
  67.         int n = 1822;
  68.         int n = -12;
  69.         int n = 2;
  70.         int n = -47;
  71.         int n = 0;
  72.          */
  73.  
  74.         boolean negNum = n < 0;
  75.         boolean oddNum = (n % 2 == 1);            /
  76.         boolean negAndOdd = negNum && oddNum;
  77.  
  78.         myWindow.writeOut("Is this number negative, and odd? " + n + ": " + negAndOdd);
  79.     }
  80.  
  81.     private void ex24(){
  82.         myWindow.clearOut();
  83.  
  84.         int n = 22;
  85.         /* //TEST VALUES
  86.         int n = 42;
  87.         int n = 39;
  88.         int n = 1822;
  89.         int n = -12;
  90.         int n = 2;
  91.         int n = -47
  92.         int n = 0;
  93.          */
  94.  
  95.         boolean x1;
  96.         boolean x2;
  97.         boolean x3;
  98.         boolean result;
  99.  
  100.         x1 = (n >= 1 && n <= 100);  //1 to 100 inclusive
  101.         x2 = (n >= 40 && n <= 50);  //40 to 50 inclusive
  102.         x3 = (n % 2 == 0);          //even numbers
  103.  
  104.         result = x1 && !(x2 && x3);      
  105.         myWindow.writeOutLine(result);
  106.     }
  107.  
  108.     private void ex25(){
  109.         myWindow.clearIn();
  110.         myWindow.clearOut();
  111.  
  112.         String userInput;
  113.         userInput = myWindow.readIn();
  114.  
  115.         boolean userResult = userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("n");
  116.         myWindow.writeOut(userResult);
  117.  
  118.     }
  119.  
  120.     private void ex26(){
  121.         myWindow.clearOut();
  122.  
  123.        
  124.         String userInput;
  125.  
  126.         userInput = "1998done";             //returns true if a String variable “userInput” does not have the value “done”, false if has done
  127. //        userInput = "done";               //prints false
  128. //        userInput = "Done";               //prints true - capital D used
  129. //        userInput = "Jasmine Kaur";       //prints true
  130.  
  131.         boolean userResult = !userInput.contains("done");
  132.  
  133.         myWindow.writeOut(userResult);
  134.  
  135.     }
  136.  
  137.     private void ex28(){
  138.         myWindow.clearOut();
  139.  
  140.         //set of test values
  141.         int year = 2016;  
  142.         //int year = 2017;
  143.         //int year = 4929;
  144.         //int year = 2028;
  145.  
  146.         boolean x1 = year % 4 == 0;              
  147.         boolean x2 = year % 100 != 0;            
  148.         boolean x3 = year % 400 == 0;            
  149.         boolean IsLeapYear;
  150.  
  151.         IsLeapYear = x1 && x2 || x3;  
  152.         myWindow.writeOutLine("Is " + year + " a leap year? " + IsLeapYear);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement