HarrJ

Day 12 samples

Jul 4th, 2023 (edited)
1,464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. public class Day12A {
  2.     public static void main(String[] args) {
  3.         int i = 1, n = 5;
  4.        
  5.         do {
  6.             System.out.println("do while: " + i);
  7.             i++;
  8.         } while (i <= n);
  9.         i = 1;
  10.         while (i <= n) {
  11.             System.out.println("while: " + i);
  12.             i++;
  13.         }
  14.         i = 1;
  15.         for (int j = i; j <= n; j++) {
  16.             System.out.println("for: " + j);
  17.         }
  18.        
  19.         // i5 n0
  20.         // i1 n1
  21.     }
  22. }
  23.  
  24. //------**********-------------
  25.  
  26.  
  27. public class Day12B {
  28.     public static void main(String[] args) {
  29.         double[] numbers = {9.81, 42, 299.1, -1, 273};
  30.         double sum = 0;
  31.        
  32.         for (double number : numbers) {
  33.             System.out.println("+ " + number);
  34.             sum += number;
  35.         }
  36.         System.out.println("= " + sum);
  37.        
  38.         System.out.println("*** for loop version ***");
  39.        
  40.         double[] numbers2 = {12, 44, 3.75, -4.44, 33};
  41.         sum = 0;
  42.         for (int i = 0; i < numbers2.length; i++) {
  43.             double number = numbers2[i];
  44.             sum += number;
  45.             if (i < (numbers2.length - 1)) {
  46.                 System.out.print(number + " + ");
  47.             } else {
  48.                 System.out.print(number + " = ");
  49.             }
  50.         }
  51.         System.out.println(sum);
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. //------**********-------------
  58.  
  59. import java.util.Scanner;
  60.  
  61. public class Day12C {
  62.     public static void main(String[] args) {
  63.         Scanner sc = new Scanner(System.in);
  64.         double newNumber = 0;
  65.         double total = 0;
  66.         //we are limited with what we have discussed
  67.         System.out.println("addition(type 0 to end)");
  68.        
  69.         do {            
  70.             System.out.print("+ ");
  71.             newNumber = sc.nextDouble();
  72.             total += newNumber;
  73.             System.out.println("= " + total);
  74.         } while (newNumber != 0);
  75.     }
  76. }
  77. //------**********-------------
  78. public class Day12D {
  79.    
  80.     public static void main(String[] args) {
  81.         //para magamit si display method, gagawa tayo ng instance
  82.         // ng class na ito
  83.         Day12D callMe = new Day12D();
  84.         callMe.display();
  85.        
  86.         //kapag static wala na initialization sa kapwa nya static
  87.         myMethod();
  88.     }
  89.    
  90.     void display() {
  91.         Day12AccessTest2 callTest2 = new Day12AccessTest2();
  92.         //import galing week1training package
  93.         System.out.println("Hello World!");
  94.         System.out.println("from this folder " + callTest2.bookTitle4);
  95.         System.out.println("from this folder " + callTest2.bookTitle5);
  96.         System.out.println("from this folder " + callTest2.bookTitle6);
  97.     }
  98.    
  99.     static void myMethod() {
  100.         System.out.println("This is a print from a static method");
  101.     }
  102. }
  103. //------**********-------------
  104.  
  105. public class Day12AccessTest2 {
  106.     private String bookTitle3 = "Jurassic Park";
  107.     public String bookTitle4 = "The Andromeda Strain";
  108.     String bookTitle5 = "Harry Potter";
  109.     protected String bookTitle6 = "The Hobbit";
  110. }
Advertisement
Add Comment
Please, Sign In to add comment