Java - Ingreso de direcciones Ip en Java NetBeans

 
Vista:

Ingreso de direcciones Ip en Java NetBeans

Publicado por Dulce (2 intervenciones) el 12/11/2014 08:36:09
Ingresar direcciones Ip en Java , clasificar según su Clase y posteriormente ordenar cada dirección ip por medio de su primer valor. Necesito el código para comparar con lo que he hecho. (Utilizar arreglos para las direcciones ip).

Run:
Ingresar direcciones Ip:
104.168.1.1
102.168.1.1
105.168.1.1
129.168.1.1
128.168.1.1
255.168.1.1
192.168.1.1
-------------------
Clase A 0-127
102.168.1.1
104.168.1.1
105.168.1.1

Clase B 128-191
128.168.1.1
129.168.1.1

Clase C 192-255
192.168.1.1
255.168.1.1
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Ingreso de direcciones Ip en Java NetBeans

Publicado por Andrés (340 intervenciones) el 13/11/2014 06:58:18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
 
class IPV4Clasification {
 
	public final static String CLASS_A = "A";
	public final static String CLASS_B = "B";
	public final static String CLASS_C = "C";
 
	private Map<String, List<IPV4>> ips;
 
	public IPV4Clasification () {
 
		ips = new HashMap<String, List<IPV4>>();
 
		ips.put(CLASS_A, new ArrayList<IPV4>());
		ips.put(CLASS_B, new ArrayList<IPV4>());
		ips.put(CLASS_C, new ArrayList<IPV4>());
 
	}
 
	public void addIPV4(IPV4 ip) {
 
		short first = ip.getFirst();
 
		if(0 <= first && first <= 127) {
			ips.get(CLASS_A).add(ip);
		}else if(128 <= first && first <= 191) {
			ips.get(CLASS_B).add(ip);
		}else if(192 <= first && first <= 255) {
			ips.get(CLASS_C).add(ip);
		}
 
	}
 
	public void sortIps() {
 
		Collections.sort(ips.get(CLASS_A));
		Collections.sort(ips.get(CLASS_B));
		Collections.sort(ips.get(CLASS_C));
 
	}
 
	public void printIps() {
 
		System.out.println("Class A");
		printIps(ips.get(CLASS_A));
		System.out.println("Class B");
		printIps(ips.get(CLASS_B));
		System.out.println("Class C");
		printIps(ips.get(CLASS_C));
 
	}
 
	public void printIps(List<IPV4> list) {
 
		for(IPV4 ip :list) {
 
			System.out.println(ip);
 
		}
 
	}
 
}
 
class IPV4 implements Comparable<IPV4> {
 
	private short first;
	private short second;
	private short third;
	private short four;
 
	private final static String IPV4_SEPARATOR = ".";
 
	public IPV4(String foo) {
 
		if(null==foo) {
 
			throw new IllegalArgumentException("foo is required!");
 
		}
 
		this.setup(foo);
 
	}
 
	private void setup(String foo) {
 
		StringTokenizer tokens = new StringTokenizer(foo, IPV4_SEPARATOR);
 
		this.first = Short.parseShort(tokens.nextToken());
		this.second = Short.parseShort(tokens.nextToken());
		this.third = Short.parseShort(tokens.nextToken());
		this.four = Short.parseShort(tokens.nextToken());
 
	}
 
	@Override
	public String toString() {
 
		StringBuilder sb =  new StringBuilder();
 
		sb.append(this.first).append(IPV4_SEPARATOR);
		sb.append(this.second).append(IPV4_SEPARATOR);
		sb.append(this.third).append(IPV4_SEPARATOR);
		sb.append(this.four);
 
		return sb.toString();
 
	}
 
	@Override
	public int compareTo(IPV4 o) {
 
 
		return  this.first == o.first ? 0 :  this.first > o.first ? 1 : this.first < o.first ? -1 : Integer.MIN_VALUE;
 
	}
 
	public short getFirst() {
		return first;
	}
 
	public void setFirst(short first) {
		this.first = first;
	}
 
	public short getSecond() {
		return second;
	}
 
	public void setSecond(short second) {
		this.second = second;
	}
 
	public short getThird() {
		return third;
	}
 
	public void setThird(short third) {
		this.third = third;
	}
 
	public short getFour() {
		return four;
	}
 
	public void setFour(short four) {
		this.four = four;
	}
 
}
 
 
public class Ipv4Test {
 
	public static void main(String[] args) {
 
		IPV4Clasification iPV4Clasification = new IPV4Clasification();
 
		iPV4Clasification.addIPV4(new IPV4("104.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("102.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("105.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("129.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("128.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("255.168.1.1"));
		iPV4Clasification.addIPV4(new IPV4("192.168.1.1"));
 
		iPV4Clasification.sortIps();
		iPV4Clasification.printIps();
 
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

Ingreso de direcciones Ip en Java NetBeans

Publicado por Dulce (2 intervenciones) el 13/11/2014 20:37:25
Gracias es diferente a lo que hice pero me sirvió al 100% ....
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Ingreso de direcciones Ip en Java NetBeans

Publicado por Cleber (1 intervención) el 18/05/2017 18:43:49
Excelente trabajo yo tuve un problema similar que aun no lo he podido generar tal vez tu me puedas ayudar el ejercio es así:
"Desarrollar un .java que permita generar aleatoriamente direcciones IP y de acuerdo
a las validaciones del caso se vaya almacenando en orden dentro de los arreglos A, B,
C que representan a la clase que pertenecen. Las direcciones pueden estar ordenadas
dentro de cada arreglo únicamente por el criterio de los primeros 8 octetos sin importar
los restantes. Implemente las excepciones que considere pertinentes. Al final lo que
se debe visualizar por pantalla son tres listados correspondientes a las tres clases de
direcciones IP; cada arreglo (lista) ordenado, y al final un informe estadistico en el cual
se indique el número total de direcciones Ips por grupo generadas, así como, cual es
el grupo que menos y mas direcciones Ips a generado.
Importante: el usuario no debe ingresar por teclado ningún valor, todo debe
autogenerarse, de forma tal que, cuando el usuario ejecute su programa, cada vez se
listen grupos/arreglos (A, B, C) de direcciones IP diferentes."
Consideraciones
"El primer número del ID de red no puede ser 127. Este número de ID está
reservado para pruebas de conexión, como realizar un bucle local.
• Los números del ID de host no pueden ser todos 255, ya que esta dirección se
utiliza como dirección de difusión IP .
• El ID de host no puede ser todo cero, ya que esta dirección se utiliza para indicar
un ID de red.
• El ID de host deber ser exclusivo para el ID de red local.

Desde ya muchas gracias
Ejercicio
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar