Coder Social home page Coder Social logo

u2-hoja-4-ejercicio-2's Introduction

U2-Hoja-4-EJERCICIO-2

   2.- Diseñar una clase llamada ‘coche’ con los atributos privados: 
       matricula y velocidad y los métodos:
   
      • Un constructor que permita declarar objetos de tipo coche inicializado con la 
        matricula y la velocidad pasados como argumentos.
     
      • Otro constructor que permita declarar objetos de tipo coche con la matricula,
        pasada como argumento y ponga la velocidad a 0.
     
      • Un método llamado acelera que incrementa la velocidad en el valor que 
        pasamos como argumento, otro llamado frena idéntico pero que disminuye la 
        velocidad, controlando siempre que la velocidad no puede ser menor de 0 ni 
        mayor de 120.
     
      • Un método que permita mostrar las letras de la matrícula.
   
      • Un método que permita mostrar la matricula y velocidad del coche.
   
 Ejemplo de ejecución:

      creo un coche con matricula y velocidad
      Coche [matricula=1234RET, velocidad=20]
      creo otro coche solo con matricula
      Coche [matricula=4567KKK, velocidad=0]
      el coche 2 despues de frenar 10 es
      Coche [matricula=4567KKK, velocidad=0]
      el coche 1 despues de acelerar 80 es
      Coche [matricula=1234RET, velocidad=20]
      Las letras que tiene la matricula del coche Coche [matricula=1234RET, velocidad=100] 
      son RET

4

 import javax.swing.JOptionPane;
public class Ejercicio2 {
    public static void main(String[] args) {
    Coche coche2=new Coche();
    String matric,matric2,veloc;     
     matric=JOptionPane.showInputDialog(">>> Crear matricula del coche: " ); 
     coche2.setMatricula(matric);  
     veloc=JOptionPane.showInputDialog(">>> Crear velocidad del coche: " );   
     coche2.setVelocidad(veloc);
     System.out.print("\n\nCreo un coche con matricula y velocidad\n");    
     System.out.print("Coche [matricula="+matric+", velocidad="+veloc+"]\n");  
     System.out.print("Creo otro coche solo con matricula\n");     
     matric2 =JOptionPane.showInputDialog(">>> Crear matricula del coche 2: " ); 
     coche2.setMatricula(matric2);   
     Coche veloCero=new Coche(); 
     veloCero.setVelocidadCero(0);
     System.out.print("Coche [matricula="+matric2+", velocidad="+veloCero.getVelocidadCero()+"]");    
     System.out.print("\nel coche 2 despues de frenar 10 es");
     int ternario = veloCero.getVelocidadCero()<0 ? 0 : 0;
     System.out.print("\nCoche [matricula="+matric2+", velocidad="+ternario+"]");
     System.out.print("\nel coche 1 despues de acelerar 80 es ");
     System.out.print("\nCoche [matricula="+matric+", velocidad=");        
     var acelera = Integer.parseInt(coche2.getVelocidad()); 
     System.out.print(acelera+80); System.out.print("]\n");    
     String sinnumeros = matric;
     String letrasMatricula = sinnumeros.replace('1',' ').replace('2','\b').replace('3',
     ' ').replace('4',' ').replace('5','\b').replace('6',' ').replace('7','\b').replace('8', 
     ' ').replace('9','\b').replace('0','\b');
     System.out.print("Las letras que tiene la matricula del coche Coche [matricula="+matric+", velocidad="+veloc+"]" +
     "\nson \b"+letrasMatricula+"\n\n");   
    }
}

1

2

3

public class Coche {
    private String matricula;
    private String velocidad;
    private int velocidadCero;
    public Coche(){}
    public Coche(String matricula, String velocidad, int velocidadCero){
       this.matricula=matricula;  
       this.velocidad=velocidad;
       this.velocidadCero=velocidadCero;} 
   public String getMatricula(){ return matricula;}
   public String getVelocidad(){ return velocidad;}
   public int getVelocidadCero(){ return velocidadCero;}
   public void setMatricula(String matric){this.matricula=matric;}
   public void setVelocidad(String veloc) {this.velocidad=veloc;}
   public void setVelocidadCero(int veloCero) {this.velocidadCero=veloCero;} 
}

u2-hoja-4-ejercicio-2's People

Contributors

limengyan22 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.