Acces an ArrayList on a different method (same class) - java

im trying to acces the ArrayList "Lista" (with the modifications it had in the "Guardar" method) in the "verJugadores" method, is this possible? When I print the ArrayList of the "seePlayer" method I notice that it is empty, However, the one on the method "save" has an element inside of it (the attributes are assigned a value on another class), here is the code:
public class Jugador extends VentanaJugador{
private int puntuacion;
String edad1, edad;
String nombre;
String nom;
ArrayList<Jugador>lista=new ArrayList<>();
Jugador hola;
public Jugador() {
super();
}
public Jugador(int puntuacion, String nombre, String edad1, ArrayList lista) {
super();
this.hola = new Jugador();
this.puntuacion = puntuacion;
this.nombre = nombre;
this.edad1 = edad1;
this.lista = lista;
}
public int getPuntuacion() {
return puntuacion;
}
public void setPuntuacion(int puntuacion) {
this.puntuacion = 0;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getEdad() {
return edad1;
}
public void setEdad(String edad1) {
this.edad1 = edad1;
}
#Override
public String toString() {
return "Nombre: " + this.getNombre() + "\n" +
"Edad: " + this.getEdad();
}
/**
*
* #param nom
* #param edad
* #return
*/
public ArrayList guardar(String nom, String edad){
nombre = nom;
edad1=edad;
this.hola = new Jugador(puntuacion, nombre, edad1, lista);
lista.add(hola);
System.out.println("*******************");
System.out.println("Nombre: "+hola.getNombre()+"\n"+
"Edad: "+hola.getEdad()+"\n"+
"Puntuacion Inicial: "+hola.getPuntuacion());
System.out.println("*******************");
return lista;
}
public void verJugadores(){
System.out.println("*******************");
System.out.println("La lista de jugadores es:"+
"\n"+
"*******************");
if(lista.isEmpty()){
System.out.println("La lista esta vacia");
}else{
for (int i=0;i<lista.size();i++) {
System.out.println("Nombre: "+lista.get(i).getNombre());
System.out.println("Edad: "+lista.get(i).getEdad());
System.out.println("Puntuacion: "+lista.get(i).getPuntuacion());
}
}
}
}

From your guardar method, call verJugadores method, and while doing so send your ArrayList, in this case lista, as a parameter.
public ArrayList guardar(String nom, String edad){
nombre = nom;
edad1=edad;
this.hola = new Jugador(puntuacion, nombre, edad1, lista);
lista.add(hola);
System.out.println("*******************");
System.out.println("Nombre: "+hola.getNombre()+"\n"+
"Edad: "+hola.getEdad()+"\n"+
"Puntuacion Inicial: "+hola.getPuntuacion());
System.out.println("*******************");
verJugadores(lista); // calling the verJugadores method.
return lista;
Then, make changes in your verJugadores accordingly.
public void verJugadores(ArrayList<Jugador> lista){ // here parameter added..
System.out.println("*******************");
System.out.println("La lista de jugadores es:"+
"\n"+
"*******************");
if(lista.isEmpty()){
System.out.println("La lista esta vacia");
}else{
for (int i=0;i<lista.size();i++) {
System.out.println("Nombre: "+lista.get(i).getNombre());
System.out.println("Edad: "+lista.get(i).getEdad());
System.out.println("Puntuacion: "+lista.get(i).getPuntuacion());
}
Since arraylist is a non-primitive data type, the parameter in the verJugadores method will refer to the same lista.
I hope I was able to answer your question..

Related

Trouble accessing data from java program

I'm having a goofy issue. I'm trying to see if I can printout the restaurants and employees data I have here and I can't remember how best to do it this.
Once I can figure out how to do that, I'll be able to create methods using it, but I can't seem to remember how to do it this way.
Updated Code
class Main {
public static void main(String[] args) {
Employee john = new Employee("John","asian",35.00);
Employee sam = new Employee("Sam","Greek",25.00);
Employee michael = new Employee("Michael","Italian",50.00);
Restaurant asian = new Restaurant("Asian","asian",25.00);
Restaurant greek = new Restaurant("greek","greek",25.00);
Restaurant italian = new Restaurant("italian","italian",25.00);
}
public static class Restaurant {
private String restaurantName;
private String cuisine;
private double price;
public Restaurant( String restaurantName,
String cuisine,
double price) {
this.restaurantName = restaurantName;
this.cuisine = cuisine;
this.price = price;
}
public String getRestaurantName() {
return restaurantName;
}
public String getCuisine() {
return cuisine;
}
public double getPrice() {
return price;
}
}
public static class Employee {
private String employeeName;
private String cuisine;
private double budget;
public Employee(String employeeName,
String cuisine,
double budget) {
this.employeeName = employeeName;
this.cuisine = cuisine;
this.budget = budget;
}
public String getEmployeeName() {
return employeeName;
}
public String getCuisine() {
return cuisine;
}
public double getBudget() {
return budget;
}
}
}
For printing out the data of an object you can override the toString method.
After that, the class Restaurant looks like this.
public static class Restaurant {
private String restaurantName;
private String cuisine;
private double price;
public Restaurant( String restaurantName,
String cuisine,
double price) {
this.restaurantName = restaurantName;
this.cuisine = cuisine;
this.price = price;
}
public String getRestaurantName() {
return restaurantName;
}
public String getCuisine() {
return cuisine;
}
public double getPrice() {
return price;
}
#Override
public String toString() {
return "Restaurant [restaurantName=" + restaurantName + ", cuisine=" + cuisine + ", price=" + price + "]";
}
}
the class Employee looks like this.
public static class Employee {
private String employeeName;
private String cuisine;
private double budget;
public Employee(String employeeName,
String cuisine,
double budget) {
this.employeeName = employeeName;
this.cuisine = cuisine;
this.budget = budget;
}
public String getEmployeeName() {
return employeeName;
}
public String getCuisine() {
return cuisine;
}
public double getBudget() {
return budget;
}
#Override
public String toString() {
return "Employee [employeeName=" + employeeName + ", cuisine=" + cuisine + ", budget=" + budget + "]";
}
}
and then you can print an object in sysout
System.out.println(michael);
You have to use a toString method() for each Object's class.
For example in Employee Class:
public String toString()
{
String str = "Employee Name: " + employeeName +
"\nCuisine: " + cuisine + "\nBudget: " + budget;
return str;
}
After that you just have to use the toString() in the main() method:
System.out.println(john.toString());
In the main method you could also use an array to store the data and make it easier to access. Then to display both of the objects inside you could use just one for loop, but I used two to keep the outputs separate from one another.
int numEmployees = 3;
Employee myEmployees[] = new Employee[numEmployees];
myEmployees[0] = new Employee("John","asian",35.00); // Stores John in index 0...
myEmployees[1] = new Employee("Sam","Greek",25.00);
myEmployees[2] = new Employee("Michael","Italian",50.00);
// Displays each object who's index is associated with the value for i
for(int i = 0; i < employees.length; i++)
System.out.println(myEmployees[i].toString());
int numRestaurants = 3;
Restaurant myRestaurants = new Restaurant[numRestaurant]
myRestaurants[0] = new Restaurant("Asian","asian",25.00); // Stores Asain in index 0...
myRestaurants[1] = new Restaurant("greek","greek",25.00);
myRestaurants[2] = new Restaurant("italian","italian",25.00);
// Displays each object who's index is associated with the value for i
for(int i = 0; i < restaurants.length; i++)
System.out.println(myRestaurants[i].toString());

Neodatis and duplicities

Neodatis create a new "Pais" when there is an equal one.
I'm working with Neodatis on a GUI. I have a database that contains "Jugadores" and "Pais." The "Jugadores" have an attribute of "Pais". When I want to add a new "Jugadores" to the database with one of the existing "Pais", recreate it in the database, since the query does not return that there is one with the same name.
GUI code:
gestionLiga gestionLiga = new gestionLiga();
gestionLiga.altaJugador(txtNombre.getText(), txtDeporte.getText(), txtCiudad.getText(), Integer.parseInt(txtEdad.getText()), gestionLiga.sacarPais(txtPais.getText()));
gestionLiga.sacarPais code:
public Pais sacarPais(String pais)
{
odb = ODBFactory.open("EQUIPOS.test");
IQuery query = new CriteriaQuery(Pais.class, Where.equal("nombre",pais));
Objects <Pais> listado = odb.getObjects(query);
if(listado.size() == 0)
{
int contador;
IQuery query2 = new CriteriaQuery(Pais.class);
Objects <Pais> listado2 = odb.getObjects(query2);
contador = listado2.size()+1;
odb.close();
return new Pais(contador, pais);
}
else
{
odb.close();
return (Pais)listado.getFirst();
}
}
gestionLiga.altaJugador code:
public void altaJugador(String nombre, String deporte, String ciudad, int edad, Pais pais)
{
odb = ODBFactory.open("EQUIPOS.test");
Jugadores jugador = new Jugadores(nombre, deporte, ciudad, edad, pais);
odb.store(jugador);
odb.close();
}
Pais class code:
class Pais
{
private int id;
private String nombre;
public Pais(){}
public Pais(int id, String nombre) {
this.id = id;
this.nombre = nombre;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String toString()
{
return this.nombre;
}
}
Jugadores class code:
public class Jugadores
{
private String nombre, deporte, ciudad;
private int edad;
private Pais pais;
public Jugadores(){}
public Jugadores(String nombre, String deporte, String ciudad, int edad, Pais pais)
{
this.nombre = nombre;
this.deporte = deporte;
this.ciudad = ciudad;
this.edad = edad;
this.pais = pais;
}
public String getNombre()
{
return nombre;
}
public void setNombre(String nombre)
{
this.nombre = nombre;
}
public String getDeporte()
{
return deporte;
}
public void setDeporte(String deporte)
{
this.deporte = deporte;
}
public String getCiudad()
{
return ciudad;
}
public void setCiudad(String ciudad)
{
this.ciudad = ciudad;
}
public int getEdad()
{
return edad;
}
public void setEdad(int edad)
{
this.edad = edad;
}
public Pais getPais() {
return pais;
}
public void setPais(Pais pais) {
this.pais = pais;
}
#Override
public String toString() {
return "NOMBRE: " + nombre + " - DEPORTE: " + deporte + " - CIUDAD: " + ciudad + " - EDAD: " + edad + " - PAIS: " + pais;
}
}
If I enter two players with the same country, the query in theory should return me that there is already a Pais with that name, which should not create a new one, however, enter the IF and create a new one.
Thanks for the help!
I just don't close odb on sacarPais, then I don't open (because I can't open it twice)on altaJugador. After insert a new Jugador with Pais reference, I close the odb. It's done, a bu*****t.

Linked lists, students to a subject

I am trying to register students and subjects, then add a student to a subject. All this using linked lists in Java.
So far I can add students and subjects and show the list of them but I’m not able to “link” them and show what students are taking what subject.
Code here:
package examenfinal;
/**
*
* #author USUARIO
*/
public class Alumno {
private int ID;
private String Nombre;
private int Carne;
public Alumno(int Id, String nombre, int carne) {
this.ID = Id;
this.Nombre = nombre;
this.Carne = carne;
}
public int getID() {
return ID;
}
public void setID(int Id) {
this.ID = Id;
}
public String getNombre() {
return Nombre;
}
public void setNombre(String nombre) {
this.Nombre = nombre;
}
public int getCarne() {
return Carne;
}
public void setCarne(int carne) {
this.Carne = carne;
}
#Override
public String toString() {
return "Alumno{" + "ID=" + ID + ", Nombre=" + Nombre + ", Carne=" + Carne + '}';
}
}
package examenfinal;
import java.util.LinkedList;
/**
*
* #author USUARIO
*/
public class Curso {
private int Codigo;
private String Nombre;
private int Ciclo;
private int Año;
private LinkedList<Alumno> Asignar;
public Curso() {
}
public Curso(int codigo, String nombre, int ciclo, int año) {
this.Codigo = codigo;
this.Nombre = nombre;
this.Ciclo = ciclo;
this.Año = año;
}
public int getCodigo() {
return Codigo;
}
public void setCodigo(int Codigo) {
this.Codigo = Codigo;
}
public String getNombre() {
return Nombre;
}
public void setNombre(String Nombre) {
this.Nombre = Nombre;
}
public int getCiclo() {
return Ciclo;
}
public void setCiclo(int Ciclo) {
this.Ciclo = Ciclo;
}
public int getAño() {
return Año;
}
public void setAño(int Año) {
this.Año = Año;
}
public void RegristrarAlumno(int carne, String nombre, int id){
Asignar.add(new Alumno(carne, nombre, id));
}
public LinkedList<Alumno> getAsignar() {
return Asignar;
}
}
package examenfinal;
import java.util.LinkedList;
/**
*
* #author USUARIO
*/
public class Universidad {
private LinkedList<Alumno> Inscritos;
private LinkedList<Curso> Cursos;
public Universidad()
{
Inscritos = new LinkedList<>();
Cursos = new LinkedList<>();
}
public void Crear_Curso(int cod_curso, String nombre, int ciclo, int año){
this.Cursos.add(new Curso(cod_curso, nombre, ciclo, año));
}
public void Crear_Alumno(int id, String nombre, int carne){
this.Inscritos.add(new Alumno(id, nombre, carne));
}
public void Asignar_Alumno_a_Curso(Curso curso, Alumno alumno){
curso.RegristrarAlumno(0, alumno.getNombre(), 0);
}
public LinkedList<Curso> getCursos(){
return Cursos;
}
public LinkedList<Alumno> getInscritos(){
return Inscritos;
}
}
package examenfinal;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.*;
/**
*
* #author USUARIO
*/
public final class InterfaceConsola {
private Alumno alumno1;
private Curso curso1;
private Universidad universidad1;
private Scanner in= new Scanner(System.in);
private int Opcion;
public void CrearCurso(){
System.out.println("INFORMACION DEL CURSO");
int Id;
String Nombre;
int Ciclo;
int Año;
System.out.println("CODIGO: ");
Id=(in.nextInt());
System.out.println("NOMBRE: ");
Nombre = (in.next());
System.out.println("CICLO: ");
Ciclo = (in.nextInt());
System.out.println("AÑO: ");
Año = (in.nextInt());
universidad1.Crear_Curso(Id, Nombre, Ciclo, Año);
}
public void CrearAlumno(){
System.out.println("\nINGRESE LA INFORMACION DEL ALUMNO");
System.out.println("\nID: ");
int Id;
String Nombre;
int Carne;
Id = (in.nextInt());
System.out.println("\nNOMBRE: ");
Nombre = (in.next());
System.out.println("\nNO. CARNE: ");
Carne = (in.nextInt());
universidad1.Crear_Alumno(Id, Nombre, Carne);
}
public void MostrarCursos()
{
System.out.println("CURSOS REGISTRADOS: ");
LinkedList<Curso> Cursos = universidad1.getCursos();
Iterator it =Cursos.iterator();
while (it.hasNext())
{
Curso cursoActual = (Curso) it.next();
System.out.println(cursoActual.getNombre());
}
}
public void MostrarAlumnos()
{
System.out.println("ALUMNOS REGISTRADOS: ");
LinkedList<Alumno> Alumnos = universidad1.getInscritos();
Iterator it =Alumnos.iterator();
while (it.hasNext())
{
Alumno AlumnoActual = (Alumno) it.next();
System.out.println(AlumnoActual.getNombre());
}
}
public void AsignarAlumnos(){
}
public int Menu()
{
System.out.println("UNIVERSIDAD MARIANO GALVEZ DE GUATEMALA");
System.out.println("\tMENU");
System.out.println("\n1. INGRESAR ALUMNO");
System.out.println("2. MOSTRAR ALUMNOS INSCRITOS");
System.out.println("3. CREAR CURSO");
System.out.println("4. MOSTRAR CURSOS");
System.out.println("5. ASIGNAR ALUMNOS POR CURSO");
System.out.println("6. MOSTRAR ALUMNOS POR CURSO");
System.out.println("7. SALIR");
System.out.println("SELECCIONE UNA OPCION: ");
return in.nextInt();
}
public InterfaceConsola(){
universidad1= new Universidad();
}
public void Operacion()
{
int opcion = Menu();
while (opcion!= 7)
{
if (opcion == 1)
CrearAlumno();
if (opcion == 2)
MostrarAlumnos();
if (opcion == 3)
CrearCurso();
if (opcion == 4)
MostrarCursos();
if (opcion == 5)
AsignarAlumnos();
opcion = Menu();
}
}
}
package examenfinal;
/**
*
* #author USUARIO
*/
public class ExamenFinal {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
InterfaceConsola inter1 = new InterfaceConsola();
inter1.Operacion();
}
}
YOu need to refactor Curso to accept an existing Alumno
public void RegristrarAlumno(Alumno alumno){
Asignar.add(alumno);
}
next in your InterfaceConsola#AsignarAlumnos you must do ask for student id,
retrieve the student from LinkedList.
public void AsignarAlumnos(){
System.out.println("\nINGRESE LA ID DEL ALUMNO");
int alumnoId = in.nextInt();
System.out.println("\nINGRESE LA ID DEL CURSO");
int cursoId = in.nextInt();
List<Alumno> insrictos = universidad1.getInscritos();
Alumno alumno = null;
for(Iterator<Alumno> iteratorDelInstrictos = instritos.iterator(); iteratorDelInsritos.hasNext();){
Alumno testAlumno = iteratorDelInsritos.next();
if(testAlumno.getId() == alumnoId ){
alumno = testAlumno;
break;
}
}
if(alumno == null){
System.out.println("Alumno not found);
return;
}
Curso curso = null;
for(Iterator<Curso> iteratorDelCursos = universidad.getCursos().iterator(); iteratorDelCursos.hasNext();){
Curso testCurso = iteratorDelCursos.next();
if(testCurso.getCodigo() == cursoId){
curso = testCurso;
break;
}
}
if(curso == null){
System.out.println("Curso not found);
return;
}
curso.RegristrarAlumno(alumno);
Also you need to follow Java naming guidelines (all variables are lowercase), and use generics for your iterators.
With a little less of understanding of the question and considering -
Alumno must be Student, and Curso is presumably Subject
Every Curso seem to have a list of Alumno attached to it.
private LinkedList<Alumno> Asignar
To modify/create this list, you need to
private Universidad universidad1 = new Universidad();
universidad1.getCursos().get(i).getAsignar().add(new Alumno());
where i would be the index of the Curso you're willing to update and you can replace new Alumno() with an instance of Alumno that you want to add to the list within Curso.

Create a simple system to students and subjects

I started to study java and i'm try to do a simple system to associate students and subjects, my problem is how can i associate one student to many subjects, i hope you can help me.
Main Class
package exercicios;
public class Exercicio3_Main {
public static void main(String[] args) {
//Criando Alunos
Exercicio3_Aluno aluno[] = new Exercicio3_Aluno[3];
aluno[0] = new Exercicio3_Aluno("Douglas", "Telematica", 201391);
//Criando as Disciplinas
Exercicio3_Disciplina disciplina[] = new Exercicio3_Disciplina[7];
disciplina[0] = new Exercicio3_Disciplina("POO");
aluno[0].cadastrarDisciplina(disciplina[0]);
//aluno[0].listarAluno();
}
}
Student Class
package exercicios;
import java.util.ArrayList;
public class Exercicio3_Aluno {
public String nome;
public String curso;
private int matricula;
private ArrayList<Exercicio3_Disciplina> disciplina;
public Exercicio3_Aluno(String nome, String curso, int matricula) {
super();
this.nome = nome;
this.curso = curso;
this.matricula = matricula;
}
public void listarAluno(){
System.out.println("------------- ALUNO --------------");
System.out.println("Nome: " + this.getNome());
System.out.println("Curso: " + this.getCurso());
System.out.println("Matricula: " + this.getMatricula());
System.out.println("Disciplinas: " + disciplina);
System.out.println("----------------------------------");
}
//Metodos Acessores
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCurso() {
return curso;
}
public void setCurso(String curso) {
this.curso = curso;
}
public int getMatricula() {
return matricula;
}
public void setMatricula(int matricula) {
this.matricula = matricula;
}
public void cadastrarDisciplina(Exercicio3_Disciplina disciplina){
this.disciplina.add(disciplina);
}
}
Subjects Class
package exercicios;
public class Exercicio3_Disciplina {
public String nome;
private float nota1;
private float nota2;
private float nota3;
private float media;
public Exercicio3_Disciplina(String nome) {
super();
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public float getNota1() {
return nota1;
}
public void setNota1(float nota1) {
this.nota1 = nota1;
}
public float getNota2() {
return nota2;
}
public void setNota2(float nota2) {
this.nota2 = nota2;
}
public float getNota3() {
return nota3;
}
public void setNota3(float nota3) {
this.nota3 = nota3;
}
public float getMedia() {
return media;
}
public void setMedia(float media) {
this.media = media;
}
}
The output of this code is something like this:
Nome: Douglas
Curso: TLM
Matrícula: 102050
Disciplinas: Programação Orientada a Objetos
What i need is list many subjects to students, i know it's probably is very simple but i started to study this now and all of this is new for me ;D
Instead of one subject, you want multiple subjects on the student so use List<Subjects> on the student class and adjust the getters and setters as such. You can then add subjects to the list on the student.
Sample code
public static void main(String[] args) {
//Criando os Alunos
Exercicio3_Aluno aluno[] = new Exercicio3_Aluno[3];
aluno[0] = new Exercicio3_Aluno("Douglas", "TLM", 102050);
//Criando as Disciplinas
Exercicio3_Disciplina disciplina[] = new Exercicio3_Disciplina[7];
disciplina[0] = new Exercicio3_Disciplina("Programação Orientada a Objetos");
disciplina[1] = new Exercicio3_Disciplina("Sistemas Operacionais");
//Mostrando os dados do Aluno
aluno[0].cadastrarDisciplina(disciplina[0]);
aluno[0].cadastrarDisciplina(disciplina[1]);
aluno[0].listaAluno();
}
In your student method
//Lista o Aluno
public void listaAluno(){
System.out.println("--------- DADOS DO ALUNO --------");
System.out.println("Nome: " + this.nome);
System.out.println("Curso: " + this.curso);
System.out.println("Matrícula: " + this.matricula);
System.out.println("Disciplinas: ");
String disciplinasString = "";
for(Exercicio3_Disciplina disciplina : this.disciplinas)
{
disciplinasString = disciplinasString + disciplina.getNome());
}
System.out.println(disciplinasString);
System.out.println("---------------------------------");
}

How to manipulate variables in deep class

I just started to learn Java and i think this a very basic question but i didn't find the right answer yet so i try in here.
I just want to show in my console the names and descriptions of some animals, but i don't know how to manipulate variables from a class of a class.
I guess i have to use parameters but i don't find how to use them in a class...
My "Animal.java"
public class Animal {
private String nom;
public static String DESCRIPTION;
public String toString(){
return "Je suis " + this.nom;
}
public void direNom(){
System.out.println(toString());
}
public void direDESCRIPTION(){
System.out.println("Description: " + this.DESCRIPTION);
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom = nom;
}
class Vertebre{
int nbrVertebre;
class Mammifere{
class Ours{
String nom = "Poumba";
String DESCRIPTION = "Description de Poumba";
}
class Chimpanze{
String nom = "Cheeta";
String DESCRIPTION = "Description de Cheeta";
}
class Rats{
String nom = "Ratata";
String DESCRIPTION = "Description de Ratata";
}
}
class Poisson{
class Requins{
String nom = "Jaws";
String DESCRIPTION = "Description de Jaws";
}
class Raies{
String nom = "Raimonta";
String DESCRIPTION = "Description de Raimonta";
}
class Truites{
String nom = "Truita";
String DESCRIPTION = "Description de Truita";
}
}
class Reptile{
class Tortue{
String nom = "Tortega";
String DESCRIPTION = "Description de Tortega";
}
class Serpent{
String nom = "Serpento";
String DESCRIPTION = "Description de Serpento";
}
}
}
}
My "TestZoo.java"
public class TestZoo {
public static void main(String[] args){
Animal unAnimal = new Animal();
unAnimal.setNom("Jaws");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
unAnimal.setNom("Cheeta");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
unAnimal.setNom("Ham");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
}
}
Try this:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone{
public static void main(String []args){
Ours poumba = new Ours();
poumba.direNom();
}
}
class Animal {
protected String nom;
public String toString(){
return "Je suis " + this.nom + ", je suis un " + this.getClass();
}
public void direNom(){
System.out.println(toString());
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom = nom;
}
}
class Vertebre extends Animal {
int nbrVertebre;
}
class Mammifere extends Vertebre {}
class Ours extends Mammifere {
public Ours(){
this.setNom("Poumba");
}
}

Categories

Resources