Created
June 10, 2011 04:51
-
-
Save fsouza/1018248 to your computer and use it in GitHub Desktop.
Arquivos de uma prova de tempos remotos :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package prova; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class Arquivo { | |
private File arquivo; | |
private Scanner leitor; | |
public Arquivo(String caminho) throws FileNotFoundException { | |
this.arquivo = new File(caminho); | |
this.leitor = new Scanner(this.arquivo); | |
} | |
public int obterInteiro() { | |
return this.leitor.nextInt(); | |
} | |
public float obterReal() { | |
return this.leitor.nextFloat(); | |
} | |
public void fechar() { | |
this.leitor.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package prova; | |
public class Numero { | |
private float num; | |
public Numero(float val) { | |
this.setNum(val); | |
} | |
public Numero(int val) { | |
this.setNum(val); | |
} | |
public static Numero Somar(Numero n1, Numero n2) { | |
return (new Numero(n1.getNum() + n2.getNum())); | |
} | |
public static Numero Subtrair(Numero n1, Numero n2) { | |
return (new Numero(n1.getNum() - n2.getNum())); | |
} | |
public float getNum() { | |
return num; | |
} | |
public void setNum(float num) { | |
this.num = num; | |
} | |
public void setNum(int num) { | |
this.num = ((float)num); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* UsaClasses.java | |
* | |
* Arquivo para testar a prova, nome de classe definida por professor. | |
*/ | |
package prova; | |
import java.io.FileNotFoundException; | |
public class UsaClasses { | |
public UsaClasses() { | |
} | |
public static void main(String[] args){ | |
try { | |
int n_int; | |
float n_float; | |
Arquivo arq = new Arquivo("c:\\arq.txt"); | |
n_int = arq.obterInteiro(); | |
n_float = arq.obterReal(); | |
arq.fechar(); | |
Numero n1 = new Numero(n_int); | |
Numero n2 = new Numero(n_float); | |
Numero n3 = Numero.Somar(n1, n2); | |
Numero n4 = Numero.Subtrair(n1, n2); | |
System.out.println("A soma dos números lidos é " + n3.getNum()); | |
System.out.println("A subtração dos números lidos é " + n4.getNum()); | |
} catch (FileNotFoundException erro) { | |
System.out.println("Arquivo não encontrado"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment