Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. ```java
  2. import java.util.Random;
  3. class Metodos {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int ano = 2004;
  8.  
  9. // geraArray
  10. // exibeArray
  11.  
  12. int n = 3;
  13. // cria um array de tamanho N
  14. // prenchido com números aleatórios
  15. // [1, 20]
  16.  
  17. int[] numeros = geraArray(n);
  18.  
  19. // [1, 2, ..., n]
  20. exibeArray(numeros);
  21.  
  22. // antes: [ 1, 2, 3]
  23. int posicao1 = 1;
  24. int posicao2 = 2;
  25. trocaPosicao(numeros, posicao1, posicao2);
  26.  
  27. exibeArray(numeros);
  28.  
  29.  
  30. }
  31.  
  32. static void trocaPosicao(int[] numeros, int pos1, int pos2) {
  33. // numeros[pos1]
  34. // numeros[pos2]
  35.  
  36. int aux;
  37. aux = numeros[pos1];
  38. numeros[pos1] = numeros[pos2];
  39. numeros[pos2] = aux;
  40. }
  41.  
  42. static int[] geraArray(int n) {
  43. int[] numeros = new int[n];
  44.  
  45. Random gerador = new Random();
  46.  
  47. for (int i = 0; i < n ; i++) {
  48. int numero = gerador.nextInt(20) + 1; // [1, 21[
  49. numeros[i] = numero;
  50. }
  51. return numeros;
  52.  
  53. }
  54.  
  55. static void exibeArray(int[] numeros) {
  56.  
  57. String texto = "[";
  58.  
  59. for(int i = 0; i < numeros.length - 1; i++) {
  60. texto += numeros[i] + ", ";
  61. }
  62. if(numeros.length != 0) {
  63. texto += numeros[numeros.length - 1] + "]";
  64. } else {
  65. texto += "]";
  66. }
  67.  
  68. System.out.println(texto);
  69.  
  70. }
  71.  
  72.  
  73. // Múltiplo de 400, é bissexto
  74. // Cc. se ele for múltiplo de 100, não é
  75. // CC, se ele for múltiplo de 4, ele é bissexto
  76. // CC, não é bissexto
  77. static boolean ehBissexto(int ano) {
  78. if(ano % 400 == 0) {
  79. return true;
  80. }
  81. if(ano % 100 == 0) {
  82. return false;
  83. }
  84. if(ano % 4 == 0) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89.  
  90. }
  91.  
  92. static void calculaBaskara(double a, double b, double c) {
  93.  
  94. double delta = b*b - 4*a*c;
  95.  
  96. if(delta < 0 || a == 0) {
  97. System.out.println("Impossível calcular");
  98. } else {
  99. double R1 = (-b - Math.sqrt(delta))/ 2 * a;
  100. double R2 = (-b + Math.sqrt(delta)) / 2* a;
  101. System.out.printf("R1 = %.5f\n", R1);
  102. System.out.printf("R2 = %.5f\n", R2);
  103. }
  104.  
  105. }
  106.  
  107. static double calculaConsumoDeCombustivel(double km, double litros) {
  108. return km/litros;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. }
  115.  
  116. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement