Facebook
From KasiaKAsia, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 216
  1. 1. arrays/print
  2.  
  3. public static void print(int[] a) {
  4.     for (int i = 0; i <a.length; i++) {
  5.         System.out.println("element " + "[" + i + "]" + " is " +  a[i]);
  6.    
  7.     }
  8.    
  9. }
  10.  
  11. 2. arrays/maxValue
  12.  
  13. public static int maxValue(int[] a) {
  14.     int max = Integer.MIN_VALUE;
  15.     for (int i = 0; i <a.length; i++) {
  16.         if (a[i] > max) {
  17.             max = a[i];
  18.         }
  19.     }
  20.     return max;
  21. }
  22.  
  23. 3. arrays.squareArray
  24.  
  25. int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  26. int[] squares = new int[array.length];
  27. for (int i = 0; i < array.length; i++) {
  28.     squares[i] = array[i] * array[i];
  29. }
  30.  
  31. 4. console output/printProgram
  32.  
  33. public class PrintProgram{
  34.    
  35.     //static String s = "public class public class Hello {";
  36.    
  37.     public static void main(String[] args) {
  38.        
  39.         System.out.println("public class Hello {");
  40.         System.out.println("    public static void main(String[] args) {");
  41.         System.out.println("        System.out.println(\"Hello, world!\");");
  42.         System.out.println("    }");
  43.         System.out.println("}");      
  44.        
  45.     }
  46. }
  47.  
  48. 5. consile output/calculate line
  49.  
  50. import java.util.Scanner;
  51. public class CalculateLine {
  52.     public static void main (String [] args) {
  53.         int x;
  54.         int y;
  55.         Scanner odczyt =  new Scanner(System.in);
  56.         System.out.println("This program calculates y coordinates for a line.");
  57.         System.out.print("Enter slope (m): ");
  58.         int slope = odczyt.nextInt();
  59.         System.out.print("Enter intercept (b): ");
  60.         int intercept = odczyt.nextInt();
  61.         System.out.print("Enter x: ");
  62.         x = odczyt.nextInt();
  63.         while (x != -1) {
  64.             y = slope*x + intercept;
  65.             System.out.println("f("+ x +") = " + y);
  66.             System.out.print("Enter x: ");
  67.             x = odczyt.nextInt();
  68.         }
  69.     }
  70. }
  71.  
  72. 6. cons out/evensumMax
  73.  
  74. import java.util.Scanner;
  75.  
  76. public static void evenSum() {
  77.     int quantity;
  78.     int integer;
  79.     int max = -1000000;
  80.     int evenSum = 0;
  81.     Scanner odczyt = new Scanner(System.in);
  82.     System.out.print("how many integers? ");
  83.     quantity = odczyt.nextInt();
  84.     for (int i = 0 ; i < quantity; i++) {
  85.         System.out.print("next integer? ");
  86.         integer = odczyt.nextInt();
  87.        
  88.         if (integer%2 == 0) {
  89.             evenSum += integer;
  90.             if (integer > max) {
  91.                 max = integer;
  92.             }
  93.         }
  94.        
  95.     }
  96.     System.out.println("even sum = " + evenSum);
  97.     System.out.println("even max = " + max);
  98. }
  99.  
  100.  
  101. 7. cons out/FearTheTree
  102.  
  103. System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
  104. System.out.println("|| FEAR THE TREE! ||");
  105. System.out.println("////////////////////");
  106.  
  107.  
  108. 8. cons out/inches to cm
  109.  
  110. import java.util.Scanner;
  111. import java.text.*;
  112.  
  113. public class InchesToCentimeters{
  114.     static public double round(double d, int ic) {
  115.         java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
  116.         nf.setMaximumFractionDigits(ic);
  117.         nf.setMinimumFractionDigits(ic);
  118.     return Double.parseDouble((nf.format(d)).replaceAll(",", ".").replaceAll(" ", "") );
  119.     }
  120.     public static void main(String[] args) {
  121.         System.out.println("This program converts feet and inches to centimeters.");
  122.         int Feet;
  123.         int Inches;
  124.         Double Feet_Inches;
  125.         Double Suma;
  126.         double Centimeters, Cm_Format;
  127.         Scanner sc = new Scanner(System.in);
  128.        
  129.         System.out.print("Enter number of feet: ");
  130.         Feet = sc.nextInt();
  131.         //int feet = Integer.parseInt(Feet);
  132.        
  133.         System.out.print("Enter number of inches: ");
  134.         Inches = sc.nextInt();
  135.         //Double inches = Double.parseDouble(Inches);
  136.        
  137.         Feet_Inches = Inches / 12.00 ;
  138.         Suma = Feet + Feet_Inches;
  139.         //Double suma = Double.parseDouble(Suma);
  140.  
  141.         Centimeters = Suma * 30.48;
  142.        
  143.        
  144.         System.out.print(Feet + "ft " + Inches + "in " + "= " + round(Centimeters,2) + "cm");
  145.     }
  146. }
  147.  
  148. 9. cons/out/number square
  149.  
  150. public static void numberSquare(int min, int max) {
  151.     int range = max - min + 1;
  152.     for (int i = 0; i < range; i ++) {
  153.         for(int j = 0; j < range; j++ ) {
  154.            
  155.             System.out.print( (i + j ) % (range) + min );
  156.         }
  157.         System.out.println();
  158.     }
  159.    
  160. }
  161.  
  162.  
  163. 10. cons out/print triangle
  164.  
  165. public static void printTriangle() {
  166.     String newline = System.lineSeparator();
  167.     for(int i=0;i<=5; i++) {
  168.         for (int j=0;j<=i;j++){
  169.             System.out.print("#");
  170.         }
  171.       System.out.println();
  172.     }
  173. }
  174.  
  175. 11. cons out/stanford vs call
  176.  
  177. import java.util.Scanner;
  178. public class StanfordVsCal{
  179.     public static void main(String[] args) {
  180.         int Stanford;
  181.         int Cal;
  182.         Scanner odczyt = new Scanner(System.in);
  183.         System.out.print("Stanford: How many points did they score? ");
  184.         Stanford = odczyt.nextInt();
  185.         System.out.print("Cal: How many points did they score? ");
  186.         Cal = odczyt.nextInt();
  187.         if (Stanford > Cal) {
  188.             System.out.println("Stanford won!");
  189.         }else {
  190.             System.out.println("Cal won!");
  191.         }
  192.  
  193.     }
  194. }
  195.  
  196.  
  197. 12. expressions / digit expressions
  198.  
  199. 10s place: number%100/10
  200. 100s place: number%1000/100
  201.  
  202. 13. expressions/displacement
  203.  
  204. import java.lang.Math;
  205.  
  206. double s0 = 12.0;
  207. double v0 = 3.5;
  208. double a = 9.8;
  209. double t = 10.0;
  210. double s = s0 + v0*t + 0.5 * a * Math.pow(10,2);
  211. System.out.print(s);
  212.  
  213.  
  214. 14. expressions/printMaxMin
  215.  
  216.         46,36,23,13
  217.    
  218. 15.expresions/times operator
  219.  
  220.     double y;
  221.     y = x*(x*(x*(12.3*x - 9.1) + 19.3) - 4.6) + 34.2;
  222.     System.out.print(y);
  223.    
  224. 16. expressions/valuesOfABC
  225.  
  226.         6,9,16
  227.    
  228. 17. expressions/valuesOfIJK
  229.  
  230.         4,2,1
  231.    
  232. 18. loops/biggerAndSmaller
  233.  
  234. import java.util.Scanner;
  235.  
  236. public class BiggestAndSmallest {
  237.     public static void main(String[] args){
  238.         Scanner sc = new Scanner(System.in);
  239.         System.out.print("Number of numbers? ");
  240.         int n = sc.nextInt();
  241.         int biggest = -100000000;
  242.         int smallest = 10000000;
  243.         for (int i = 1; i <=n; i++) {
  244.             System.out.print("Number " + i +": ");
  245.             int number = sc.nextInt();
  246.             if (number>biggest) {
  247.                 biggest = number;
  248.             }
  249.             if (number < smallest) {
  250.                 smallest = number;
  251.             }
  252.         }
  253.        
  254.         System.out.println("Biggest = " + biggest);
  255.         System.out.println("Smallest = " + smallest);
  256.     }
  257. }
  258.  
  259. 19. loops/ComputeSumOfDigits
  260.  
  261. import java.util.Scanner;
  262.  
  263. public class ComputeSumOfDigits {
  264.     public static void main(String[] args){
  265.         int integer, Int;
  266.         int suma = 0;
  267.        
  268.        
  269.        
  270.         Scanner odczyt = new Scanner(System.in);
  271.         System.out.print("Type an integer: ");
  272.         integer = odczyt.nextInt();
  273.         int length = (int)(Math.log10(integer)+1);
  274.        
  275.        
  276.         while (integer > 0) {
  277.            
  278.             suma += integer%10;
  279.             integer = integer/10;
  280.             //System.out.println("Digit sum is: ");
  281.            
  282.         }
  283.         System.out.println("Digit sum is " + suma);
  284.        
  285.     }
  286. }
  287.  
  288. 20. loops/FizzBuzz
  289.  
  290. import java.util.Scanner;
  291.  
  292. public class FizzBuzz {
  293.     public static void main(String[] args) {
  294.         int max;
  295.         double a;
  296.         Scanner odczyt = new Scanner(System.in);
  297.         System.out.print("Max value? ");
  298.         max = odczyt.nextInt();
  299.         a = max / 20.0;
  300.         for (int i = 0; i < a; i++) {
  301.             for (int j = 0; j < 20; j++) {
  302.                 if ((i*20 + j + 1)%15==0) {
  303.                     System.out.print("FizzBuzz ");
  304.                 } else if ((i*20 + j + 1)%5==0) {
  305.                     System.out.print("Buzz ");
  306.                 }else if ((i*20 + j + 1)%3==0) {
  307.                     System.out.print("Fizz ");
  308.                 } else {
  309.                     System.out.print(i*20 + j + 1);
  310.                     System.out.print(" ");
  311.                 }
  312.                
  313.                
  314.                 if ((i*20 + j + 1)==max ) {
  315.                     break;
  316.                 }
  317.             }
  318.             System.out.println();
  319.         }
  320.     }  
  321. }
  322.  
  323. 21. loops/numberLoops1
  324.  
  325. for (int i = 1; i<=5; i++){
  326.     for (int j = 1; j <=i; j++) {
  327.         print(i);
  328.     }
  329.     println();
  330. }
  331.  
  332. 22. loops/numberLoops2
  333.  
  334. for (int i = 1; i <= 5; i++) {
  335.     for (int j = 1; j <= 5; j ++) {
  336.         if (j < 6-i){
  337.             print(".");
  338.         }
  339.         else {
  340.             print(i);
  341.         }
  342.     }
  343.     println();
  344. }
  345.  
  346. 23. loops/numberLoops3
  347.  
  348. for (int i = 1; i <= 5; i++){
  349.     for (int j = 5; j >= 1; j--) {
  350.         if (i == j) {
  351.             print(i);
  352.         }
  353.         else {
  354.             print(".");
  355.         }
  356.     }
  357.     println();
  358. }
  359.  
  360.  
  361. 24. loops/numberSquare
  362.  
  363. import java.util.Scanner;
  364.  
  365.  
  366. public class NumberSquare{  
  367.     public static void main(String[] args) {
  368.         int min, max;
  369.         Scanner odczyt = new Scanner(System.in);
  370.        
  371.         System.out.print("Min? ");
  372.         min = odczyt.nextInt();
  373.        
  374.         System.out.print("Max? ");
  375.         max = odczyt.nextInt();
  376.         int range = max - min + 1;
  377.        
  378.         for (int i = 0; i < range; i ++) {
  379.             for(int j = 0; j < range; j++ ) {
  380.                 System.out.print( (i + j ) % (range) + min );
  381.             }
  382.             System.out.println();
  383.         }
  384.     }
  385. }
  386.  
  387.  
  388. 25. loops/sentinelSum
  389.  
  390. import java.util.Scanner;
  391.  
  392. public class SentinelSum {
  393.     public static void main(String[] args) {
  394.         int number, suma=1;
  395.         Scanner sc = new Scanner(System.in);  
  396.         do {
  397.             System.out.print("Type a number: ");
  398.             number = sc.nextInt();
  399.             suma += number;
  400.         } while (number != -1);    
  401.         System.out.print("Sum is " + suma);
  402.     }
  403. }
  404.  
  405.  
  406. 26. parameters/boxOfStars
  407.  
  408. public static void boxOfStars(int width, int hight){
  409.     for (int i = 0; i < hight; i++) {
  410.         for (int j = 0; j < width; j++) {
  411.             if (i == 0) {
  412.                 System.out.print("*");
  413.             } else if ((i > 0 && i < hight - 1) && (j == 0 || j == width -1)) {
  414.                 System.out.print("*");
  415.             } else if ( i == hight -1) {
  416.                 System.out.print("*");
  417.             }else {
  418.                 System.out.print(" ");
  419.             }
  420.         }
  421.         System.out.println();
  422.     }
  423. }
  424.  
  425. 27. parameters/countDigits
  426.  
  427. public static int countDigits(int Number) {
  428.     String number = Integer.toString(Number);
  429.     if (Number > 0) {
  430.         return number.length();
  431.     } else{
  432.         return number.length() - 1;
  433.     }  
  434. }
  435.  
  436.  
  437. 28. parameters/investment
  438.  
  439. import java.util.Scanner;
  440.  
  441.  
  442. public class Investment {
  443.     public static Double FinalAmount (Double PV, double r, int n) {
  444.         Double FV;
  445.         FV = PV*Math.pow(1+r, n);
  446.         return FV;
  447.     }
  448.    
  449.     public static Double Profit(Double PV, Double FV) {
  450.         return FV - PV;
  451.     }  
  452.    
  453.     public static double CalcPercent(double PV, double Profit) {
  454.        
  455.         return (Profit-PV)  / PV* 100.00;
  456.     }
  457.    
  458.     public static String Category(double Percent){
  459.        
  460.         if (Percent >= 0 && Percent < 10) {
  461.             return "weak";
  462.         }else if (Percent >= 10 && Percent < 50) {
  463.             return "medium";
  464.         } else {
  465.             return "strong";
  466.         }
  467.     }
  468.    
  469.     public static void Oblicz(String inwestor, Scanner odczyt) {
  470.         Double PV,r, Profit;
  471.         int n;
  472. //        Scanner odczyt = new Scanner(System.in);
  473.        
  474.         System.out.println("Investor #" + inwestor + ":");
  475.        
  476.         System.out.print("Initial amount? ");
  477.         PV = odczyt.nextDouble();
  478.        
  479.         System.out.print("Interest rate%? ");
  480.         r = odczyt.nextDouble();
  481.        
  482.         System.out.print("Num. of months? ");
  483.         n = odczyt.nextInt();
  484.        
  485.         Double finalAmount = FinalAmount(PV, r, n);
  486.         System.out.printf("Final amount = $%.2f\n", finalAmount);
  487.        
  488.         Double profit = Profit(PV, finalAmount);
  489.         Double Percent = CalcPercent(PV, finalAmount);
  490.        
  491.         System.out.printf("Profit = $%.2f (%.0f%%)\n", profit, Percent);
  492.         System.out.println(Category(Percent));
  493.     }
  494.    
  495.     public static void main(String[] args) {
  496.         Scanner odczyt = new Scanner(System.in);
  497.         Oblicz("1", odczyt);
  498.         System.out.println();
  499.         Oblicz("2", odczyt);
  500.         System.out.println();
  501.         System.out.println("Have a nice day!");
  502.        
  503.     }
  504.    
  505. }
  506.  
  507.  
  508. 29. parameters/printpay
  509.  
  510. public static void printPay(Double Salary, int Hours) {
  511.     Double Earnings;
  512.     int Difference;
  513.     if (Hours <= 8) {
  514.         Earnings = Salary * Hours;
  515.     } else {
  516.         Difference = Hours - 8;
  517.         Earnings = 1.5*Salary *Difference + 8*Salary;
  518.     }
  519.     System.out.println("Hours worked: " + Hours);
  520.     System.out.printf("Pay earned: $" + String.format( "%.2f", Earnings ) );
  521. }
  522.  
  523.  
  524.  
  525. 30. parameters/triangle
  526.  
  527. public static void triangle(int size){
  528.     for (int i = 0; i < size; i ++){
  529.         for (int j = 1; j <= size; j++) {
  530.             if (j < size  - i) {
  531.                 System.out.print(" ");
  532.             }else {
  533.                 System.out.print("*");
  534.             }
  535.         }
  536.         System.out.println();
  537.     }
  538.    
  539.    
  540. }
  541.  
  542.  
  543. 31. parameters/getDaysMonth
  544.  
  545. public static int getDaysInMonth(int Month) {
  546.     int [] Days = {31,28,31,30,31,30,31,31,30,31,30,31};
  547.        
  548.     return Days[Month - 1];
  549. }
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.