Exercício de Fixação; Classes, Objetos e Métodos em Java
Autor: Thiago Campos de Campos | Publicado: 2013-12-31T09:12:00-08:00


O
&nbsp; objetivo &nbsp; deste exercício é fixar o conceito de classes e objetos, métodos &nbsp; e&nbsp;atributos.&nbsp;Dada&nbsp;a&nbsp;estrutura&nbsp;de&nbsp;uma&nbsp;classe,&nbsp;basta&nbsp;traduzi-­la&nbsp;para&nbsp;a&nbsp;linguagem&nbsp;Java&nbsp;e&nbsp;fazer&nbsp;uso&nbsp;de
um&nbsp;objeto&nbsp;da&nbsp;mesma&nbsp;em&nbsp;um&nbsp;programa&nbsp;simples.



Crie uma classe Conta com os atributos&nbsp;int numero,&nbsp;Cliente dono,&nbsp;double saldo e&nbsp;double limite, e outra classe Cliente com os atributos&nbsp;String nome,&nbsp;String sobreNome e&nbsp;String cpf. Na classe Conta crie um método saca(), deposita() e outro transferirPara(). Crie uma classe com main para testar as demais classes.



Código Java



//CLASSE CONTA&nbsp;-----------------------------------------------------------------------------------

//Autor: Thiago Campos

//Data: 31.12.2013



package Banco;



/**

&nbsp;*

&nbsp;* @author
Thiago

&nbsp;*/

public class Conta {

&nbsp;&nbsp;&nbsp; //declaraçao
dos variaveis (atributos)

&nbsp;&nbsp;&nbsp; double conta;

&nbsp;&nbsp;&nbsp; double saldo;

&nbsp;&nbsp;&nbsp; double limite;

&nbsp;&nbsp;&nbsp; Cliente dono = new
Cliente();

&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp; //Metodo
sacar

&nbsp;&nbsp;&nbsp; boolean
sacar(double valor){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(this.saldo
&lt; valor){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
false;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.saldo
-= valor;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
true;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; } //fim
do medtod sacar

&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp; //metodo
depositar

&nbsp;&nbsp;&nbsp; void
depositar(double valor){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//declaração da variavel local

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.saldo =
this.saldo + valor;

&nbsp;&nbsp;&nbsp; } //fim
do metodo depositar

&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp; //metodo
transferiri

&nbsp;&nbsp;&nbsp; boolean
transferirPara(Conta cDestino, double valor) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//declara a variavel de referencia e atribui valor

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
retirou = this.sacar(valor);



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (retirou ==
false) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
false;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
cDestino.depositar(valor);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
true;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } //fim
do else

&nbsp;&nbsp;&nbsp; }






























































































} //fim da classe



//CLASSE CLIENTE&nbsp;-----------------------------------------------------------------------------------

//Autor: Thiago Campos

//Data: 31.12.2013



package Banco;



/**

&nbsp;*

&nbsp;* @author
Thiago

&nbsp;*/

public class Cliente {

&nbsp;&nbsp;&nbsp; //declaração
das variaveis

&nbsp;&nbsp;&nbsp; String nome;

&nbsp;&nbsp;&nbsp; String sobreNome;

&nbsp;&nbsp;&nbsp; String cpf;

&nbsp;&nbsp;&nbsp; 
































} //fim da classe cliente



//CLASSE PROGRAMA -----------------------------------------------------------------------------------

//Autor: Thiago Campos

//Data: 31.12.2013



package Banco;



import javax.swing.JOptionPane;



/**

&nbsp;*

&nbsp;* @author
Thiago

&nbsp;*/

public class Programa {

&nbsp;&nbsp;&nbsp; public static void
main(String args[]){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //declaração
do objeto

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Conta c1;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Conta c2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean
confirmacao; //vai receber a confirmação do metodo

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cliente cli;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//criação de uma instancia do tipo Conta

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1 = new
Conta();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2 = new
Conta();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cli = new
Cliente();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //uso
dos atributos da conta

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.dono.nome =
"Thiago";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.conta =
111;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.limite =
200;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1.saldo =
100;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.dono.nome =
"Rafael";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.conta =
222;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.limite =
300;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.saldo =
1000;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //uso o
metodo sacar

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Como
só ha um comando no bloco if e no else o uso das chaves é facultativo

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; confirmacao =
c1.sacar(60);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if(confirmacao)JOptionPane.showMessageDialog(null, "Consegui
sacar\nSaldo: " + c1.saldo); 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;else JOptionPane.showMessageDialog(null,
"Não consegui sacar");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//transferir: uso dos metodos sacar e depositar

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; confirmacao =
c2.transferirPara(c1, 400);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if(confirmacao)JOptionPane.showMessageDialog(null, "Consegui
transferir"); 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else
JOptionPane.showMessageDialog(null, "Não consegui transferir");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //uso do
metodo depositar

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
c1.depositar(80);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
c2.depositar(50);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //saida
dos dados

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
JOptionPane.showMessageDialog(null, c1.dono.nome + " : " +
c1.saldo + "\n" + c2.dono.nome + " : " + c2.saldo);

&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp; } //fim
do main

&nbsp;&nbsp;&nbsp; 
















































































































} //fim da classe