...
 
Commits (2)
......@@ -217,4 +217,6 @@ public class Attack {...}
```
:warning: **Try to do it yourself first**.
\ No newline at end of file
:warning: **Try to do it yourself first**.
---
\ No newline at end of file
*.class
\ No newline at end of file
public class Arrayprod extends Arraysum {
protected double prod;
// Recompute the productory
private void recomputeProd(){
this.prod = 1.0;
for(int i=0;i<array.length;i++){
this.prod *= array[i];
}
}
// Constructor
public Arrayprod(int[] arr){
// Call parent constructor
super(arr);
// Compute productory
recomputeProd();
}
// To set a value
public void setVal(int p, int v){
if(array[p]!=0){
this.prod /= array[p];
super.setVal(p,v);
this.prod *= v;
}else{
super.setVal(p,v);
recomputeProd();
}
}
// Override toString method
public String toString(){
return super.toString() + " (prod: "+this.prod+")";
}
}
\ No newline at end of file
public class Arraysum{
protected int[] array;
protected int sum;
// Constructor
public Arraysum(int[] arr){
this.array = arr.clone();
this.sum = 0;
for(int i=0;i<array.length;i++){
this.sum += array[i];
}
}
// Copy constructor
public Arraysum(Arraysum other){
this.sum = other.sum;
this.array = new int[other.array.length];
for(int i=0;i<other.array.length;i++)
this.array[i] = other.array[i];
}
// To get a value
public int getVal(int p){
return this.array[p];
}
// To set a value
public void setVal(int p, int v){
this.sum -= array[p];
this.array[p] = v;
this.sum += array[p];
}
// To get the sum
public int getSum(){
return this.sum;
}
// Override toString method
public String toString(){
StringBuilder sb = new StringBuilder("[");
for(int i=0; i<this.array.length; i++){
sb.append(this.array[i]);
if(i<this.array.length-1) sb.append(",");
}
sb.append("] (sum: "+this.sum+")");
return sb.toString();
}
// Override equals method
public boolean equals(Arraysum other){
if(this.sum != other.sum){
return false;
}
if(this.array.length != other.array.length){
return false;
}
for(int i=0; i<this.array.length; i++){
if(this.array[i]!=other.array[i]){
return false;
}
}
return true;
}
}
\ No newline at end of file
public class Test1 {
public static void main(String[] args) {
int[] arr = {10,20,30,40,50};
Arraysum asum = new Arraysum(arr);
System.out.println(asum);
Arrayprod aprod = new Arrayprod(arr);
aprod.setVal(0,200);
System.out.println(aprod);
}
}
\ No newline at end of file
......@@ -105,7 +105,7 @@ Arraysum@6bc7c054
---
```
```java
public class Arraysum{
// :
// :
......@@ -224,15 +224,51 @@ Equals? true
---
![](humor/object_oriented_programming_ugly.png)
# Constructor de copia
:arrow_right: Es un constructor que inicializa una instancia utilizando otra de la misma clase.
```java
public class Arraysum{
// :
// :
// Copy constructor
public Arraysum(Arraysum other){
this.sum = other.sum;
this.array = new int[other.array.length];
for(int i=0;i<other.array.length;i++)
this.array[i] = other.array[i];
}
}
```
---
# Referencia `super`
```java
public class Test3 {
public static void main(String[] args){
int[] arr = {10,20,30};
Arraysum asum1 = new Arraysum(arr);
:arrow_right: En los constructores de clases derivadas, podemos usar `super()` para ejecutar el constructor de la clase **padre**.
:arrow_right: También podemos usar `super.` para referirnos a funciones y atributos como están definidos en la clase padre.
$\Rightarrow$ Es equivalente a `this` pero como si tuviera el tipo de la clase padre.
Arraysum asum2 = new Arraysum(asum1);
asum1.setVal(0,110);
System.out.println(asum1);
System.out.println(asum2);
}
}
```
```plain
user@pc:~/poo$ java Test3
[110,20,30] (sum: 160)
[10,20,30] (sum: 60)
```
---
![](humor/object_oriented_programming_ugly.png)
---
......@@ -255,12 +291,20 @@ class MultiDerived(Base1, Base2):
---
# Referencia `super`
:arrow_right: En los constructores y métodos de clases derivadas, podemos usar `super()` para ejecutar el método de la clase **padre**.
:arrow_right: También podemos usar `super.` para referirnos a métodos y atributos como están definidos en la clase padre.
$\Rightarrow$ Es equivalente a `this` pero como si tuviera el tipo de la clase padre.
---
# :soccer: Actividad
:arrow_right: Creemos la clase `Arrayprod` que además de guardar la suma, guarda la **productoria**.
```java
public class Test3 {
public class Test {
public static void main(String[] args) {
int[] arr = {10,20,30,40,50};
Arraysum asum = new Arraysum(arr);
......@@ -272,7 +316,7 @@ public class Test3 {
}
```
```plain
user@pc:~/poo$ java Test3
user@pc:~/poo$ java Test
[10,20,30,40,50] (sum: 150)
[200,20,30,40,50] (sum: 340) (prod: 2.4E8)
```
......
No preview for this file type
No preview for this file type