quinta-feira, 25 de maio de 2017

Objeto No para implementar a Lista Ligada.

public class No {
 int valor;
 String payload;
 No prox;
 No (int valor, String payload) {
  this.valor=valor;
  this.payload=payload;
 }
}

Implemente a interface Busca usando busca binária.

public interface Busca {
 /**retorna o indice do elemento que contém o valor buscado.*/
 int busca (int valorBuscado);
}

Implemente a interface Dicionario usando uma lista ligada

public interface Dicionario {

 /** Insere como primeiro elemento da lista. */
 void insere (int valor, String payload);
 /** Remove o elemento que contém o valor buscado, retorna o payload. 
  * Não pode "quebrar" a lista. */
 String remove (int valorBuscado);
 /** Busca o valor, retorna o payload ou null caso não encontre. */
 String busca (int valorBuscado);
 /** Imprime os elementos da lista. */
 void print ();

}

quinta-feira, 4 de maio de 2017

código 2 aula de 04.05.2017 turma 3

import java.util.Random;

// one class needs to have a main() method
public class Penalti
{
  
  static int placarJogador = 0;
  static int placarGoleiro = 0;
  static int[][] gol = new int[3][5];

  static void imprimeGol() {
    System.out.println();
    for(int i=0; i < gol.length; i++) {
      for(int j=0; j < gol[import java.lang.Math; // headers MUST be above the first class
i].length; j++) {
        System.out.print(gol[i][j] + " ");
     }
      System.out.println();
    }
    
    System.out.println("PLACAR = JOGADOR " + placarJogador + " GOLEIRO " + placarGoleiro);
  }
 
  static void limpaGol() {
    for(int i=0; i < gol.length; i++) {
      for(int j=0; j < gol[i].length; j++) {
        gol[i][j] = 0;
     }
    }
  }
  
  
  static boolean defendeu(Jogador j, Goleiro g) {
    if(g.coluna == j.coluna) {
    System.out.println("DEFENDEU");
      placarGoleiro++;
      return true;
    }
    System.out.println("GOOOL");
    placarJogador++;
    return false;
  }
  

  public static void main(String[] args)
  {
   imprimeGol();
    Jogador rafinha = new Jogador();
    for (int i=0;i<5;i++) {
    //rafinha.chuta(0,0);
    rafinha.chuta();
    imprimeGol();
    Goleiro cassio = new Goleiro();
    //cassio.defende(0,0);
    cassio.defende();
    imprimeGol();
    defendeu(rafinha, cassio);
    imprimeGol();
    limpaGol();
    }    
  }
  
}

public class Jogador {
  
 // int[][] gol = Penalti.gol;
  int linha;
  int coluna;
  String nome;
  
  void chuta (int linha, int coluna) {
   this.linha = linha;
    this.coluna = coluna;
    Penalti.gol[linha][coluna] = 1;
          }
  void chuta () {
     Random r = new Random ();
      linha=r.nextInt (Penalti.gol.length);
      coluna=r.nextInt (Penalti.gol[0].length);
    Penalti.gol[linha][coluna] = 1;
  }
  
  public Jogador (){
    
  nome = "Hulk chuta";
  }
}

public class Goleiro {
int linha ;
 int coluna;
  
  void defende(int linha, int coluna) {
  this.linha = linha;
  this.coluna = coluna;
    Penalti.gol[linha][coluna] = 2;
  }
  void defende () {
     Random r = new Random ();
      linha=r.nextInt (Penalti.gol.length);
      coluna=r.nextInt (Penalti.gol[0].length);
      Penalti.gol[linha][coluna] = 2;
    for (int u = Penalti.gol.length-1 ; u >= 0; u--){
    Penalti.gol[u][coluna] = 2;
    
    }
  }

}

codigo 04.05.2017 - turma 3

//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************

import java.lang.Math; // headers MUST be above the first class

// one class needs to have a main() method
public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    OtherClass myObject = new OtherClass("Hello World!");
    System.out.print(myObject);
    /**/
    Classe c = new Classe();
    System.out.println (c.acesso()[2].toString());
    
  }
}

public class Classe {
   private OtherClass o = new OtherClass ("Classe");
  OtherClass[] acesso () {
    OtherClass[] array;
    array = new OtherClass[10];
    array[2]= new OtherClass ();
     return array;
  }
  
}

// you can add other public classes to this editor in any order
public class OtherClass
{
  private String message;
  private boolean answer = false;
  public OtherClass () {
    super();
  }
  public OtherClass(String input)
  {
    message = "Why, " + input + " Isn't this something?";
  }
  public String toString()
  {
    return message;
  }
}

codigo 04.05.2017 turma 2

//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************

import java.lang.Math; // headers MUST be above the first class

// one class needs to have a main() method
public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    OtherClass myObject = new OtherClass("Hello World!");
    System.out.print(myObject);
    System.out.println (myObject.message);
    
    Batata b = new Batata();
    System.out.println (b.retornaConteudo().message);
    System.out.println (b.retornaS());
    
    MeuObjeto meuObjeto = new MeuObjeto();
  }
}

class MeuObjeto {
  // atributos 
  // metodos
}


public class Batata {
    OtherClass o = new OtherClass ("Batata");
  OtherClass retornaConteudo () {
    return o;
  }
  String retornaS () {
    return o.toString();
  }

}
// you can add other public classes to this editor in any order
public class OtherClass
{
  public String message;
  private boolean answer = false;
  public OtherClass(String input)
  {
    message = "Why, " + input + " Isn't this something?";
    //return message;
  }
  public String toString()
  {
    return message;
  }
}

quinta-feira, 20 de abril de 2017

código 20.04.2017 turma 3

//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************

import java.lang.Math; // headers MUST be above the first class

// one class needs to have a main() method
public class JogoDaVelha
{
  char [][] tabuleiro;
  
  // verificar se ganhou, deu velha,...
  
  
  JogoDaVelha() {
    tabuleiro = new char[3][3];
    for (int i=0;i<3;i++) {
        for (int j=0;j<3;j++) {
          tabuleiro[i][j]=' ';
        }
    }
  }
  
  boolean venceu(){
   
    for(int a = 0; a<3; a++){
      if(tabuleiro[a][0] == tabuleiro[a][1] && tabuleiro[a][0] ==  tabuleiro[a][2]) {
        if(tabuleiro[a][0] != ' '){
          System.out.println("Sorte(?)");
          return true;
        }
      } 
    }
  return false;
  }
    
  
  void imprime () {
    for (int i=0;i<3;i++) {
        for (int j=0;j<3;j++) {
          System.out.print (tabuleiro[i][j]);
          if (j<2) System.out.print ("|");
        }
      if (i<2) System.out.println ("\n------");
    }   
    System.out.println ("\n");
  }
  void colocaPeca (char figura, int i, int j) {
    if(tabuleiro[i][j] == ' '){
    tabuleiro[i][j]= figura;
    } else{ 
      System.out.println("Já foi fera!");
      }
  }
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
      System.out.println ("xxx");
      JogoDaVelha jogo;
      jogo = new JogoDaVelha();
      jogo.imprime();
      jogo.colocaPeca ('x', 0,0);
      jogo.colocaPeca('x',0,1);
      jogo.colocaPeca('x', 0,2);
      jogo.venceu();
      jogo.imprime();
  }  
}