Documentos de Académico
Documentos de Profesional
Documentos de Cultura
telecomunicacin
CARRERA: Ingeniera sistemas
PROGRAMACIN I
NOMBRE
DOCENTE
MATERIA
PROGRAMACIN I
SIGLA
INF
GRUPO
# 14
120
JAVA
public class Lista {
private int cantElem;
private int max;
private int elem[];
public Lista() {
this.max = 100;
this.cantElem = 0;
this.elem = new int[max];
}
public void insertar(int x) {
this.elem[this.cantElem] = x;
this.cantElem = this.cantElem + 1;
}
public String toString() {
String s1 = "[";
int i = 0;
while (i < this.cantElem) {
if (i == this.cantElem - 1) {
1
Silvia Carvajal Mendez
s1 = s1 + this.elem[i];
} else {
s1 = s1 + this.elem[i] + " ";
}
i++;
}
s1 = s1 + "]";
return s1;
}
public int mayor() {
int may = this.elem[0], i = 0;
while (i < this.cantElem) {
if (this.elem[i] > may) {
may = this.elem[i];
}
i = i + 1;
}
return may;
}
public int menor() {
int men = this.elem[0], i = 0;
while (i < this.cantElem) {
2
Silvia Carvajal Mendez
}
return f;
}
public boolean seEncuentra(int x) {
return this.frecuencia(x) > 0;
}
public boolean diferentes() {
int i = 0;
while (i < this.cantElem) {
if (this.frecuencia(this.elem[i]) > 1) {
return false;
}
i++;
}
return true;
}
public boolean iguales() {
return this.frecuencia(this.elem[0]) == this.cantElem - 1;
}
public int indexOf(int x) {
int i = 0;
while (i < this.cantElem) {
4
Silvia Carvajal Mendez
if (this.elem[i] == x) {
return i;
}
i++;
}
return -1;
}
public int indexOf(int x, int i) {
i++;
}
return sum;
}
public boolean ascendente() {
int i = 0;
while (i < this.cantElem - 1) {
if (this.elem[i] > this.cantElem + 1) {
return false;
}
i++;
}
return true;
}
public boolean descendente() {
int i = 0;
while (i < this.cantElem - 1) {
if (this.elem[i] < this.cantElem + 1) {
return false;
}
i++;
}
6
Silvia Carvajal Mendez
return true;
}
public boolean ordenado() {
return this.ascendente() || this.descendente();
}
public int promedio() {
return this.suma() / this.cantElem;
}
public boolean par(int x) {
return x % 2 == 0;
}
}
return false;
}
public boolean existeImpar() {
int i = 0;
while (i < this.cantElem) {
if (impar(this.elem[i])) {
return true;
}
i++;
}
return false;
}
public boolean todoPares() {
int i = 0;
while (i < this.cantElem) {
if (!par(this.elem[i])) {
return false;
}
i++;
}
return true;
8
Silvia Carvajal Mendez
}
public boolean todoImpares() {
int i = 0;
while (i < this.cantElem) {
if (!impar(this.elem[i])) {
return false;
}
i++;
}
return true;
}
public boolean existeParImpar() {
return this.existePar() && this.existeImpar();
}
public boolean primo(int x) {
int i = 1;
int sum = 0;
while (i <= x) {
if (x % i == 0) {
sum = sum + 1;
}
i++;
9
Silvia Carvajal Mendez
}
return sum == 2;
}
public boolean alterno() {
int i = 0;
while (i < this.cantElem - 1) {
if (!((par(this.elem[i]) && impar(this.elem[i + 1])) || (par(this.elem[i + 1]) &&
impar(this.elem[i])))) {
return false;
}
i++;
}
return true;
}
return false;
}
public boolean todosPrimo() {
int i = 0;
while (i < this.cantElem) {
if (!primo(this.elem[i])) {
return false;
}
i++;
}
return true;
}
public int aleatoria (int a, int b)
{
return a + (int)((b-a) * Math.random());
}
public void generar_random (int a, int b, int n ){
int i=1;
while (i<=n)
{
this.insertar(aleatoria(a, b));
i=i+1;
11
Silvia Carvajal Mendez
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
12
Silvia Carvajal Mendez
Module Module1
Private Property L1 As Lista1
Sub Main()
L1 = New Lista1
L1.insertarElemento(1)
L1.insertarElemento(2)
L1.insertarElemento(30)
L1.insertarElemento(4)
L1.insertarElemento(19)
L1.insertarElemento(1)
L1.insertarElemento(20)
Console.WriteLine(L1.Descargar())
Console.WriteLine("menor diferencia de x")
Console.WriteLine(L1.menor_dif(20))
Console.WriteLine("mayor diferencia de x")
Console.WriteLine(L1.mayor_dif(4))
Console.ReadLine()
13
Silvia Carvajal Mendez
End Sub
14
Silvia Carvajal Mendez
15
Silvia Carvajal Mendez
End Function
Public Function asc() As Boolean
Dim i = 0
While i < Me.cantElem - 1
If Me.elem(i) > Me.elem(i + 1) Then
Return False
End If
i = i + 1
End While
Return True
End Function
Public Function des() As Boolean
Dim i = 0
While i < Me.cantElem - 1
If Me.elem(i) < Me.elem(i + 1) Then
Return False
End If
i = i + 1
End While
Return True
End Function
Public Sub insertarLugarAsc(ByVal x As Integer)
Dim i As Integer = Me.cantElem - 1
While i >= 0 AndAlso x < Me.elem(i)
Me.elem(i + 1) = Me.elem(i)
i = i - 1
End While
Me.elem(i + 1) = x
Me.cantElem = Me.cantElem + 1
End Sub
Public Sub insertarLugarDes(ByVal x As Integer)
Dim i = Me.cantElem - 1
While i >= 0 AndAlso x > Me.elem(i)
Me.elem(i + 1) = Me.elem(i)
i = i - 1
End While
Me.elem(i + 1) = x
Me.cantElem = Me.cantElem + 1
End Sub
Public Sub cargarAsc(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)
Dim i = 1
While i <= n
Me.insertarLugarAsc(aleatorio(a, b))
i = i + 1
End While
End Sub
Public Sub cargarDes(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)
Dim i = 1
While i <= n
Me.insertarLugarDes(aleatorio(a, b))
i = i + 1
End While
End Sub
Public Sub cargarRandom(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)
Dim i = 0
While i < n
Me.elem(i) = aleatorio(a, b)
i = i + 1
16
Silvia Carvajal Mendez
End While
End Sub
Public Sub cargarDif(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)
Dim i = 0
Dim j = 0
While i < n
j = aleatorio(a, b)
If Me.frecuencia(j) = 0 Then
Me.cantElem = Me.cantElem + 1
Me.elem(i) = j
i = i + 1
End If
End While
End Sub
Public Sub cargarIguales(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)
Dim i = 0
Dim j = 0
Dim k = aleatorio(a, b)
While i < n
j = aleatorio(a, b)
If j = k Then
Me.cantElem = Me.cantElem + 1
Me.elem(i) = j
i = i + 1
End If
End While
End Sub
Public Function aleatorio(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + Rnd() * b
End Function
Public Function indexOf(ByVal x As Integer, ByVal i As Integer) As Integer
While i < Me.cantElem
If Me.elem(i) = x Then
Return i
End If
i = i + 1
End While
Return -1
End Function
Public Function indexOf(ByVal x As Integer)
Return Me.indexOf(x, 0)
End Function
Public Function lastIndexOf(ByVal x As Integer, ByVal i As Integer) As Integer
Dim k = Me.cantElem - 1
While k >= i
If Me.elem(k) = x Then
Return k
End If
k = k - 1
End While
Return -1
End Function
Public Sub reemplazar(ByVal x As Integer, ByVal y As Integer)
Dim i = 0
While i < Me.cantElem
If Me.indexOf(x) > 0 Then
Me.elem(i + 1) = y
End If
17
Silvia Carvajal Mendez
i = i + 1
End While
End Sub
Public Function seEncuentra(ByVal x As Integer) As Boolean
Return Me.indexOf(x) >= 0
End Function
Public Function exiteFrec(ByVal n As Integer) As Boolean
Return Me.frecuencia(n) = n
End Function
Public Function par(ByVal i As Integer) As Boolean
Return Me.elem(i) Mod 2 = 0
End Function
Public Function impar(ByVal i As Integer) As Boolean
Return Not Me.par(i)
End Function
Public Function pares() As Boolean
Dim i = 0
While i < Me.cantElem
If Me.impar(i) Then
Return False
End If
i = i + 1
End While
Return True
End Function
Public Function cantidadPar() As Integer
Dim i = 0
Dim c = 0
While i < Me.cantElem
If (Me.par(i)) Then
c = c + 1
End If
i = i + 1
End While
Return c
End Function
Public Function parImpar() As Boolean
Dim i = 0
While i < Me.cantElem
If (Me.par(i)) AndAlso (Me.impar(i + 1)) Or (Me.impar(i)) AndAlso (Me.par(i + 1))
Then
Return True
End If
i = i + 1
End While
Return False
End Function
End Class
End Module
18
Silvia Carvajal Mendez
19
Silvia Carvajal Mendez