Está en la página 1de 20

Facultad: Ciencias de la computacin y

telecomunicacin
CARRERA: Ingeniera sistemas

PROGRAMACIN I

NOMBRE

SILVIA CARVAJAL MENDEZ

DOCENTE

LIC. EDWIN VARGAS YAPURA

MATERIA

PROGRAMACIN I

SIGLA

INF

GRUPO

# 14

120

Ejercicios de listas en java Y VISUAL STUDIO

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

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

if (this.elem[i] < men) {


men = this.elem[i];
}
i = i + 1;
}
return men;
}
public int dif(int x, int y) {
if (x > y) {
return x - y;
} else {
return y - x;
}
}
public int frecuencia(int x) {
int i = 0;
int f = 0;
while (i < this.cantElem) {
if (this.elem[i] == x) {
f++;
}
i++;
3
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

}
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

Santa Cruz Noviembre 2013

if (this.elem[i] == x) {
return i;
}
i++;
}
return -1;
}
public int indexOf(int x, int i) {

while (i < this.cantElem) {


if (this.elem[i] == x) {
return i;
}
i++;
}
return -1;
}
public int suma() {
int i = 0;
int sum = 0;
while (i < this.cantElem) {
sum = sum + this.elem[i];
5
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

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;
}

public boolean impar(int x) {


return !par(x);
}
public boolean existePar() {
int i = 0;
while (i < this.cantElem) {
if (par(this.elem[i])) {
return true;
}
i++;
7
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

}
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

Santa Cruz Noviembre 2013

}
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

Santa Cruz Noviembre 2013

}
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;
}

public boolean existePrimo() {


int i = 0;
while (i < this.cantElem) {
if (primo(this.elem[i])) {
return true;
}
i++;
}
10
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

}
}
}

public class Practico{

public static boolean primo(int x) {


int i = 1;
int sum = 0;
while (i <= x) {
if (x % i == 0) {
sum = sum + 1;
}
i++;
}
return sum == 2;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
12
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

Lista L1 = new Lista();


L1.insertar(18);
L1.insertar(19);
L1.insertar(22);
L1.insertar(14);
System.out.println(L1.menor());
System.out.println(L1);
}
}
VISUAL

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

Santa Cruz Noviembre 2013

End Sub

Public Class Lista


Private max As Integer
Private cantElem As Integer
Private elem() As Integer
Public Sub New(ByVal max As Integer)
Me.max = max
Me.cantElem = 0
ReDim Me.elem(max)
End Sub
Public Sub New()
Me.max = 20
Me.cantElem = 0
ReDim Me.elem(20)
End Sub
Public Overrides Function ToString() As String
Dim s As String = "["
Dim i As Integer = 0
While i < Me.cantElem
If i = Me.cantElem - 1 Then
s = s & Me.elem(i)
Else
s = s & Me.elem(i) & ", "
End If
i = i + 1
End While
s = s & "]"
Return s
End Function
Public Sub insertarIesimo(ByVal x As Integer, ByVal i As Integer)
Dim k As Integer = Me.cantElem - 1
While k >= i
Me.elem(k + 1) = Me.elem(k)
k = k - 1
End While
Me.elem(i) = x
Me.cantElem = Me.cantElem + 1
End Sub
Public Sub insertarPrim(ByVal x As Integer)
Me.insertarIesimo(x, 0)
End Sub
Public Sub insertarUlt(ByVal x As Integer)
Me.insertarIesimo(x, Me.cantElem)
End Sub
Public Sub eliminarIesima(ByVal i As Integer)
Dim k As Integer = i + 1
While k < Me.cantElem
Me.elem(k - 1) = Me.elem(k)
k = k + 1
End While
Me.cantElem = Me.cantElem + 1
End Sub
Public Sub eliminarPrim()
Me.eliminarIesima(0)
End Sub

14
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

Public Sub eliminarUlt()


Me.eliminarIesima(Me.cantElem - 1)
End Sub
Public Function mayorElem() As Integer
Dim i = 0
Dim m = Me.elem(i)
While i < Me.cantElem
If Me.elem(i) > m Then
m = Me.elem(i)
End If
i = i + 1
End While
Return m
End Function
Public Function menorElem() As Integer
Dim i = 0
Dim m = Me.elem(i)
While i < Me.cantElem
If Me.elem(i) < m Then
m = Me.elem(i)
End If
i = i + 1
End While
Return m
End Function
Public Function iguales() 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 diferentes() As Boolean
Dim i = 0
While i < Me.cantElem
If Me.frecuencia(Me.elem(i) > 1) Then
Return False
End If
i = i + 1
End While
Return True
End Function
Public Function frecuencia(ByVal x As Integer) As Integer
Dim i = 0
Dim c = 0
While i < Me.cantElem
If Me.elem(i) = x Then
c = c + 1
End If
i = i + 1
End While
Return c
End Function
Public Function ordenado() As Boolean
Return Me.asc() Or Me.des

15
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

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

Santa Cruz Noviembre 2013

19
Silvia Carvajal Mendez

Santa Cruz Noviembre 2013

También podría gustarte