Alex9090

JAVA LAB 7

Apr 13th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication19;
  7. import java.util.Arrays;
  8. import java.lang.Integer;
  9. import java.util.Comparator;
  10. /**
  11. *
  12. * @author Student
  13. */
  14. public class JavaApplication19 {
  15.  
  16. /**
  17. * @param args the command line arguments
  18. */
  19. public static void main(String[] args) {
  20. Integer [] v = {10, 13, 9, 5, 352, 25, 19, 8, 1000, 9999};
  21. Arrays.sort(v, new Comparator<Integer>(){;
  22.  
  23. @Override
  24. public int compare(Integer c1, Integer c2) //o clasa care poate fi apelata si instanta o singura data in acest context
  25. {
  26. if(c1.toString().length() < c2.toString().length())
  27. return -1;
  28. return 1;
  29. }
  30. });
  31.  
  32. for(int i = 0; i < v.length; i++)
  33. System.out.println(v[i]);
  34. }
  35.  
  36. }
  37.  
  38. -------------------------------------------------------------------------------------------
  39.  
  40. ENUMERATIE
  41. /*
  42. * To change this license header, choose License Headers in Project Properties.
  43. * To change this template file, choose Tools | Templates
  44. * and open the template in the editor.
  45. */
  46. package javaapplication21;
  47.  
  48. /**
  49. *
  50. * @author Student
  51. */
  52. public enum CoffeSize {
  53. BIG(100), HUGE(200), OVERWHELMING(300)
  54. {
  55. void afisare()
  56. {
  57. System.out.println("b");
  58. }
  59. };
  60.  
  61. CoffeSize(int ml){
  62. this.ml = ml;}
  63.  
  64. public int getMl() {
  65. return ml;
  66. }
  67.  
  68.  
  69. private int ml;
  70.  
  71. void afisare(){
  72. System.out.println("A");}
  73.  
  74. }
  75.  
  76.  
  77. -------------------------------------------------------------------------------------
  78.  
  79. /*
  80. * To change this license header, choose License Headers in Project Properties.
  81. * To change this template file, choose Tools | Templates
  82. * and open the template in the editor.
  83. */
  84. package JavaApplication24;
  85.  
  86. import java.io.FileNotFoundException;
  87.  
  88. /**
  89. *
  90. * @author Student
  91. */
  92. public class ExpExample {
  93.  
  94. /**
  95. */
  96. public void couldThrowExp() throws RuntimeException {
  97. throw new RuntimeException();
  98. }
  99.  
  100. public void couldThrowExp2() throws FileNotFoundException {
  101. throw new FileNotFoundException();
  102. }
  103.  
  104. public static void main(String[] args) {
  105.  
  106. try {
  107. ExpExample expExample = new ExpExample();
  108.  
  109. expExample.couldThrowExp();
  110. expExample.couldThrowExp2();
  111. } catch (Exception e) { //nu putem pune un exception e inainte de un runtimeException
  112. System.out.println("Expectie prinsa filenotfound");
  113. } catch (RuntimeException e) {
  114. System.out.println("Expectie prinsa runtimeexp");
  115. }
  116.  
  117. }
  118. }
  119.  
  120. -----------------------------------------------------------------------
  121.  
  122. /*
  123. * To change this license header, choose License Headers in Project Properties.
  124. * To change this template file, choose Tools | Templates
  125. * and open the template in the editor.
  126. */
  127. package lab7;
  128.  
  129. import java.io.File;
  130. import java.io.FileNotFoundException;
  131. import java.io.FileWriter;
  132. import java.io.IOException;
  133. import java.util.ArrayList;
  134. import java.util.Collections;
  135. import java.util.Scanner;
  136. import java.util.logging.Level;
  137. import java.util.logging.Logger;
  138.  
  139. /**
  140. *
  141. * @author Student
  142. */
  143. public class Fisier {
  144.  
  145. public static void main(String[] args) throws FileNotFoundException, IOException
  146. {
  147. File file = new File("in.txt");
  148. Scanner sc = new Scanner (file);
  149. ArrayList<Integer> list = new ArrayList<Integer>();
  150. while(sc.hasNext())
  151. {
  152. list.add(sc.nextInt());
  153. }
  154. Collections.sort(list);
  155. list.forEach(elem->System.out.println(elem));
  156.  
  157. FileWriter scc = new FileWriter("out.txt");
  158.  
  159. list.forEach(elem->{
  160. try {
  161. scc.write(elem.toString());
  162. } catch (IOException ex) {
  163. Logger.getLogger(Fisier.class.getName()).log(Level.SEVERE, null, ex);
  164. }
  165. });
  166. scc.close();
  167.  
  168. }
  169.  
  170. }
  171.  
  172. Citire din fisier
Advertisement
Add Comment
Please, Sign In to add comment