Guest User

Untitled

a guest
Jul 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. package ipaddress;
  2.  
  3. import java.util.StringTokenizer;
  4. import javax.swing.JOptionPane;
  5. import java.lang.Math;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. String adres = "";
  11. adres = JOptionPane.showInputDialog("Podaj adres wraz z maską sieci");
  12. StringTokenizer adres1 = new StringTokenizer(adres, "./");
  13. int ileSieci = Integer.parseInt(JOptionPane.showInputDialog("Podaj liczbę podsieci"));
  14. int ileHostow = Integer.parseInt(JOptionPane.showInputDialog("Podaj liczbę hostów"));
  15. int p1 = 0;
  16. int p2 = 0;
  17. int p3 = 0;
  18. int p4 = 0;
  19. int mask = 0;
  20. if (adres1.hasMoreTokens()) {
  21. p1 = Integer.parseInt(adres1.nextToken());
  22. System.out.println(p1);
  23. }
  24. if (adres1.hasMoreTokens()) {
  25. p2 = Integer.parseInt(adres1.nextToken());
  26. System.out.println(p2);
  27. }
  28. if (adres1.hasMoreTokens()) {
  29. p3 = Integer.parseInt(adres1.nextToken());
  30. System.out.println(p3);
  31. }
  32. if (adres1.hasMoreTokens()) {
  33. p4 = Integer.parseInt(adres1.nextToken());
  34. System.out.println(p4);
  35. }
  36. if (adres1.hasMoreTokens()) {
  37. mask = Integer.parseInt(adres1.nextToken());
  38. System.out.println(mask);
  39. if (mask == 8) {
  40. System.out.println("klasa A");
  41. } else if (mask == 16) {
  42. System.out.println("klasa B");
  43. } else if (mask == 24) {
  44. System.out.println("klasa C");
  45. int potega = 1;
  46. int jakaPotega = 8;
  47. while (potega < ileSieci) {
  48. potega *= 2;
  49. jakaPotega--;
  50. }
  51. potega -= 2;
  52. int koniec = (int) (256 - (Math.pow(2, jakaPotega)));
  53. String maskaPodsieci = 255 + "." + 255 + "." + 255 + "." + koniec;
  54. System.out.println(maskaPodsieci);
  55. int ile = 0;
  56. for (int i = 0; i < ileSieci; i++) {
  57. System.out.println("Adres sieci:\n" + p1 + "." + p2 + "." + p3 + "." + ile);
  58. System.out.println("Adresy hostów:\n");
  59. for (int j = 1; j < ileHostow; j++) {
  60. System.out.println(p1 + "." + p2 + "." + p3 + "." + j);
  61. }
  62. System.out.println("Adres Broadcastu:\n" + p1 + "." + p2 + "." + p3 + "." + ileHostow);
  63. }
  64. } else {
  65. System.out.println("Podałeś złą maskę");
  66.  
  67. }
  68. }
  69. }
  70. }
Add Comment
Please, Sign In to add comment