quarta-feira, 2 de novembro de 2016

Maior - menor : Pares e ímpares em array

Neste código temos um bom exemplo de como separar números
 pares e ímpares num array de inteiros.
O programa mostra ainda o menor e o maior número ímpar
e o menor e o maior número par.
As impressões foram feitas usando a biblioteca
javafx, a avançada e poderosa biblioteca do java.

Veja abaixo uma imagem do programa em execução:


import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class PROJETO extends Application {
     Stage stage;
     Scene scene;

     @Override
     public void start ( Stage stage ) {
          stage.show ( );
          scene = new Scene ( new Group ( ), 760, 600, Color.BLACK );
          ObservableList < Node > content = ( ( Group ) scene.getRoot ( ) )
                     .getChildren ( );
          stage.setScene ( scene );
          stage.setTitle ( "IMPRIMINDO VETOR" );
          content.add ( Vetor ( ) );
          content.add ( Vetor_1 ( ) );
          content.add ( Vetor_2 ( ) );
     }

     static Node Vetor ( ) {
          String tudo = "";
          String tudo_3 = "";
          String tudo_4 = "";
          String tudo_5 = "";
          String tudo_6 = "";
          String tudo_7 = "";
          String tudo_8 = "";
          int b = 0, f = 0, v = 50, x;
          int i, maior = 0, menor = 0;
          boolean xx = true;
          int n = 100, a = 0;
          int reais [ ] = new int [ n ];
          for ( i = 1; i <= n; i++ ) {
                reais [ a ] = i;
                a++;
          }
          for ( i = 0; i < n; i++ ) {
                tudo = tudo + reais [ i ];
                if ( i == 19 ) {
                     tudo += ( "\n" );
                }
                if ( i == 39 )
                     tudo += ( "\n" );
                if ( i == 59 )
                     tudo += ( "\n" );
                if ( i == 79 )
                     tudo += ( "\n" );
                tudo += ( "  " );
          }
          Group g = new Group ( );
          Rectangle r = new Rectangle ( );
          r.setX ( -20 );// lado esquerdo
          r.setY ( 25 );// cima
          r.setWidth ( 700 );// lado direito
          r.setHeight ( 550 );// baixo
          r.setFill ( Color.WHITE );
          Text t = new Text ( );
          t.setText ( tudo );
          t.setFill ( Color.BLACK );
          t.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
          t.setX ( 25 );
          t.setY ( 100 );
          g.setCache ( true );
          g.getChildren ( ).add ( r );
          g.getChildren ( ).add ( t );
          g.setTranslateX ( 50 );
          // //////////////////////////////////////////////////////////////////////
          Text tt = new Text ( );
          tt.setFill ( Color.BLACK );
          tt.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
          tt.setX ( 25 );
          tt.setY ( 340 );
          g.getChildren ( ).add ( tt );
          Text t_t = new Text ( );
          t_t.setFill ( Color.RED );
          t_t.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
          t_t.setX ( 210 );
          t_t.setY ( 340 );
          g.getChildren ( ).add ( t_t );
          // //////////////////////////////////////////////////////////////////////
          Text tt_1 = new Text ( );
          tt_1.setFill ( Color.BLACK );
          tt_1.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
          tt_1.setX ( 25 );
          tt_1.setY ( 320 );
          g.getChildren ( ).add ( tt_1 );
          Text tt_2 = new Text ( );
          tt_2.setFill ( Color.RED );
          tt_2.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
          tt_2.setX ( 210 );
          tt_2.setY ( 320 );
          g.getChildren ( ).add ( tt_2 );
          if ( xx ) {
                Text tt_3 = new Text ( );
                Text tt_4 = new Text ( );
                Text tt_5 = new Text ( );
                Text tt_6 = new Text ( );
                Text tt_7 = new Text ( );
                Text tt_8 = new Text ( );
                Text tt_9 = new Text ( );
                Text tt_10 = new Text ( );
                Text tt_11 = new Text ( );
                Text tt_12 = new Text ( );
                Text tt_13 = new Text ( );
                Text tt_14 = new Text ( );
                for ( x = 1; x <= 100; x++ ) {
                     tt_3.setText ( "Abaixo os 50 números pares entre 1 e 100" );
                     tt_3.setFill ( Color.GREEN );
                     tt_3.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                     tt_3.setX ( 170 );
                     tt_3.setY ( 230 );
                     if ( x % 2 == 0 ) {
                          tudo_3 = tudo_3 + x;
                          tt_4.setText ( tudo_3 );
                          tt_4.setFill ( Color.RED );
                          tt_4.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                          tt_4.setX ( 25 );
                          tt_4.setY ( 270 );
                          if ( x == 52 ) {
                               tudo_3 += ( "\n" );
                          }
                     }
                     if ( x % 2 == 1 ) {
                          tudo_4 = tudo_4 + x;
                          tt_6.setText ( "Abaixo os 50 números ímpares entre 1 e 100" );
                          tt_6.setFill ( Color.GREEN );
                          tt_6.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                          tt_6.setX ( 170 );
                          tt_6.setY ( 330 );
                          tudo_3 += ( " " );
                          tt_7.setText ( tudo_4 );
                          tt_7.setFill ( Color.BLUE );
                          tt_7.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                          tt_7.setX ( 25 );
                          tt_7.setY ( 370 );
                          if ( x == 51 ) {
                               tudo_4 += ( "\n" );
                          }
                          tudo_4 += ( " " );
                     }
                }
                for ( x = 1; x <= 100; x++ ) {
                     if ( x % 2 == 0 ) {
                          for ( i = 1; i <= v; i++ )
                               reais [ f ] = x;
                          f++;
                          menor = reais [ 0 ];
                          for ( i = 1; i < v; i++ )
                               if ( reais [ i ] < menor )
                                     menor = reais [ i ];
                          maior = reais [ 0 ];
                          for ( i = 1; i < v; i++ )
                               if ( reais [ i ] > maior )
                                     maior = reais [ i ];
                     }
                }
                tudo_5 = tudo_5 + menor;
                tudo_6 = tudo_6 + maior;
                tt_5.setText ( "O menor número par é " );
                tt_5.setFill ( Color.GREEN );
                tt_5.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_5.setX ( 170 );
                tt_5.setY ( 420 );
                tt_8.setText ( tudo_5 );
                tt_8.setFill ( Color.RED );
                tt_8.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_8.setX ( 390 );
                tt_8.setY ( 420 );
                tt_9.setText ( "O maior número par é " );
                tt_9.setFill ( Color.GREEN );
                tt_9.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_9.setX ( 170 );
                tt_9.setY ( 440 );
                tt_10.setText ( tudo_6 );
                tt_10.setFill ( Color.RED );
                tt_10.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_10.setX ( 390 );
                tt_10.setY ( 440 );
                for ( x = 1; x <= 100; x++ ) {
                     if ( x % 2 == 1 ) {
                          reais [ b ] = x;
                          b++;
                          menor = reais [ 0 ];
                          for ( i = 1; i < v; i++ )
                               if ( reais [ i ] < menor )
                                     menor = reais [ i ];
                          maior = reais [ 0 ];
                          for ( i = 1; i < v; i++ )
                               if ( reais [ i ] > maior )
                                     maior = reais [ i ];
                     }
                }
                tudo_7 = tudo_7 + menor;
                tudo_8 = tudo_8 + maior;
                tt_11.setText ( "O menor número ímpar é " );
                tt_11.setFill ( Color.GREEN );
                tt_11.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_11.setX ( 170 );
                tt_11.setY ( 460 );
                tt_12.setText ( tudo_7 );
                tt_12.setFill ( Color.RED );
                tt_12.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_12.setX ( 390 );
                tt_12.setY ( 460 );
                tt_13.setText ( "O maior número ímpar é " );
                tt_13.setFill ( Color.GREEN );
                tt_13.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_13.setX ( 170 );
                tt_13.setY ( 480 );
                tt_14.setText ( tudo_8 );
                tt_14.setFill ( Color.RED );
                tt_14.setFont ( Font.font ( null, FontWeight.BOLD, 18 ) );
                tt_14.setX ( 390 );
                tt_14.setY ( 480 );
                g.getChildren ( ).add ( tt_3 );
                g.getChildren ( ).add ( tt_4 );
                g.getChildren ( ).add ( tt_5 );
                g.getChildren ( ).add ( tt_6 );
                g.getChildren ( ).add ( tt_7 );
                g.getChildren ( ).add ( tt_8 );
                g.getChildren ( ).add ( tt_9 );
                g.getChildren ( ).add ( tt_10 );
                g.getChildren ( ).add ( tt_11 );
                g.getChildren ( ).add ( tt_12 );
                g.getChildren ( ).add ( tt_13 );
                g.getChildren ( ).add ( tt_14 );
          }
          return g;
     }

     // //////////////////////////////////////////////////////////////////////////
     static Node Vetor_1 ( ) {
          Group g = new Group ( );
           String Msg = "MUITO OBRIGADO";
          DropShadow ds = new DropShadow ( );
          ds.setOffsetY ( 3.0f );
          ds.setColor ( Color.color ( 0.4f, 0.4f, 0.4f ) );
          Text t = new Text ( );
          t.setEffect ( ds );
          t.setCache ( true );
          t.setX ( -60.0f );
          t.setY ( 550.0f );
          t.setFill ( Color.RED );
          t.setText ( Msg );
          t.setFont ( Font.font ( null, FontWeight.BOLD, 26 ) );
          g.setCache ( true );
          g.setEffect ( new Bloom ( ) );
          g.getChildren ( ).add ( t );
          g.setTranslateX ( 350 );
          return g;
     }

     // //////////////////////////////////////////////////////////////////////////
     static Node Vetor_2 ( ) {
          Group g = new Group ( );
          String Msg = "IMPRIMINDO VETOR";
          Rectangle r = new Rectangle ( );
          DropShadow ds = new DropShadow ( );
          ds.setOffsetY ( 3.0f );
          ds.setColor ( Color.color ( 0.4f, 0.4f, 0.4f ) );
          Text t = new Text ( );
          t.setEffect ( ds );
          t.setCache ( true );
          t.setX ( 230.0f );
          t.setY ( 60.0f );
          t.setFill ( Color.RED );
          t.setText ( Msg );
          t.setFont ( Font.font ( null, FontWeight.BOLD, 22 ) );
          g.setCache ( true );
          g.setEffect ( new Bloom ( ) );
          g.getChildren ( ).add ( r );
          g.getChildren ( ).add ( t );
          g.setTranslateX ( 50 );
          return g;
     }

     public static void main ( String [ ] args ) {
          Application.launch ( args );
     }
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.