Ce forum est maintenant fermé, seule cette archive statique reste consultable.
  FORUM Rue-Montgallet.com
  Programmation
  Java

  Programme java fonction

Bas de page
Auteur Sujet :

Programme java fonction

n°15410
Memphis
Franc Archer
Profil : Habitué
Posté le 09-12-2005 à 21:15:43  
 

Salut all,
 
voila quelque longues heures que je passe sur un sujet (oui je sais un jour moi aussi j' en rirai quand je verrai quelqu'un dans mon cas, mais bon, pour l' instant c'est moi le noob) et je n'arrive pas ce que je devrait arriver.
 
voici le sujet
 

Citation :

Ecrire un programme JAVA permettant de générer un tracé de la fonction y = exp(-0.1*t)*sin(0.5*t) pour une imprimante, en  utilisant un astérisque pour chacun des points du tracé. Le tracé s' effectura verticalement, de haut en bas, avec un point par ligne.


 
alors voici ma démarche, celle ue j'ai choisi de suivre, jugée valide par mon prof:
 
1°) Calculer les valeurs de Y, et les ranger dans un tableau.
 
2°) Pour chaque valeur de Y, repérer la position correspondante de l' astérisque sur la ligne. La capacité d' une ligne étant exprimé en caractère, la position sera donc une valeur entière.
 
3°) Afficher l' astérisque (càd afficher le bon nombre d' espace puis l' astérisuqe).
 
voilà mon code pour le moment et ce qui en ressort ne ressemble en rien à ce que je dois atteindre, (ps. je code sous eclipse)
 

Code :
  1. public class fonction {
  2. /**
  3.  * @param args
  4.  */
  5. public static void main(String[] args) {
  6.  // TODO Auto-generated method stub
  7.  double tab[]= new double[100];
  8.  int tb[]= new int[100];
  9.  int t=0;
  10.         int i=0;
  11.         int j=0;
  12.   
  13.    while(t<100){
  14.     tab[t]=(Math.exp(-0.1*t))*(Math.sin(0.5*t));
  15.     t=t+1;
  16.    } t=0;
  17.    while(i<100){
  18.     tb[i]=((int)tab[i])+30;
  19.     i=i+1;
  20.    } i=0;
  21.    while(j<100){
  22.    for(i=0;i<=tb[j];i++){
  23.     if (i!=tb[j]) System.out.println(" " );
  24.     else System.out.println("*" );
  25.    }
  26.        j=j+1;
  27.    }
  28.   
  29.   
  30. }
  31. }


 
Merci quand même d' avance


Message édité par Memphis le 09-12-2005 à 21:16:17
mood
Pub
Posté le 09-12-2005 à 21:15:43  
 

n°15411
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 00:37:32  
 

Ah en effet c'est pas la bonne methode je pense, je vais essayer ca :D dans la soirée ou demain :D


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15412
Memphis
Franc Archer
Profil : Habitué
Posté le 10-12-2005 à 00:44:50  
 

merci à toi!
 
en effet tout est bon sauf le principal, l' affichage, sinon bon, la fonction et la rentrer dans un tableau ca va!

n°15413
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 00:49:37  
 

Bon vais me lancer sur l'affichage vite fait :d


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15414
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 01:23:40  
 

Voilà à l'arrache pour la partie graphique, ya deux fichiers  
 
1er fichier  
 

Code :
  1. /*
  2. * Repere.java
  3. *
  4. * Created on 10 décembre 2005, 00:52
  5. *
  6. * To change this template, choose Tools | Options and locate the template under
  7. * the Source Creation and Management node. Right-click the template and choose
  8. * Open. You can then make changes to the template in the Source Editor.
  9. */
  10. package tracecourbe;
  11. import java.awt.Canvas;
  12. import java.awt.Checkbox;
  13. import java.awt.Color;
  14. import java.awt.Graphics;
  15. import java.awt.event.MouseEvent;
  16. import java.awt.event.MouseListener;
  17. import java.awt.event.MouseMotionListener;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextField;
  22. import javax.swing.SwingUtilities;
  23. class FenetrePrin extends JFrame{
  24.      Cartesien RepereCartesien;
  25.         static int Int_Ligne_Horizontal_1 = 0;
  26.         static int Int_Ligne_Horizontal_2 = 0;
  27.         static int Int_Ligne_Horizontal_Fin = 0;
  28.         static int Int_Ligne_Vertical_1 = 0;
  29.         static int Int_Ligne_Vertical_2 = 0;
  30.         static int Int_Ligne_Vertical_Fin = 0;
  31.         static double Db_x_Charge1 = 50;
  32. static double Db_x_Pt = 0;
  33. static double Db_y_Pt = 0;
  34. static int Valider_OK_1 = 0;
  35. static int Valider_OK_2 = 0;
  36. static int TRACER_Courbe = 0;
  37. static int TRACER_Charge = 0;
  38. static int sauver_OK = 0;
  39.         static JButton Tracer = new JButton("Tracer" ); // bouton tracer
  40.         static JPanel Panel_Options = new JPanel(null);//panel qui contiendra les options
  41.         static JTextField Field = new JTextField("50.0" );//saisi de texte
  42.         static Checkbox Checkbox = new Checkbox ("Valider",false);
  43.    
  44.     FenetrePrin(){
  45.                     setBounds(60,60,840,620);
  46.                     setTitle("GRAPHIQUE  " );
  47.                     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.                     // On crée et place les objets dans la fenêtre
  49.                     RepereCartesien = new Cartesien();
  50.                     RepereCartesien.setBounds(420,10,401,401);
  51.                     getContentPane().add(RepereCartesien);
  52.                     Tracer.setBounds(10,120,100,25);
  53.                     Checkbox.setBounds(310,15,120,20);
  54.                     Field.setBounds(160,70,200,15);
  55.                     Panel_Options.add(Tracer);
  56.                     Panel_Options.add(Field);
  57.                     Panel_Options.add(Checkbox);
  58.                     //Panel_Options.setBorder(BorderFactory.createTitledBorder("Options" ));
  59.                     Panel_Options.setBounds(10,410,401,160);
  60.                     getContentPane().add(Panel_Options);
  61.                     this.setVisible(true);
  62.                    
  63.     }   
  64. class Cartesien extends Canvas {
  65. // Constructeur
  66. Cartesien(){}
  67.  // Méthode paint : pour le graphisme
  68.  public void paint (Graphics g)
  69.  {
  70.   setBounds(420,10,401,401);
  71.   setBackground(Color.white);
  72.   // On ajoute un MouseListener pour le comportement de la souris DANS le repère
  73.   addMouseListener(new MouseListener()
  74.     {
  75.    public void mouseClicked(MouseEvent me)
  76.    {
  77.     if (SwingUtilities.isLeftMouseButton(me))
  78.      {leftClickAction(me);}
  79.     else
  80.      {rightClickAction(me);}
  81.    }
  82.    protected void rightClickAction(MouseEvent me){}
  83.    protected void leftClickAction(MouseEvent me){}
  84.    public void mouseEntered(MouseEvent me) {}
  85.      public void mouseExited(MouseEvent me) {}
  86.      public void mousePressed(MouseEvent me) {}
  87.      public void mouseReleased(MouseEvent me) {}
  88.     }
  89.   );
  90.   addMouseMotionListener(new MouseMotionListener()
  91.     {
  92.    public void mouseMoved (MouseEvent me)
  93.    {
  94.    }
  95.    public void mouseDragged(MouseEvent me) {}
  96.     }
  97.   );
  98.    // On trace les lignes de fond
  99.    Int_Ligne_Horizontal_Fin = 0; Int_Ligne_Horizontal_1 = 0;  Int_Ligne_Horizontal_2 = 0;
  100.    Int_Ligne_Vertical_Fin = 0;  Int_Ligne_Vertical_1 = 0;  Int_Ligne_Vertical_2 = 0;
  101.    while(Int_Ligne_Horizontal_Fin <= 40)
  102.    {
  103.     g.setColor(Color.lightGray);
  104.     g.drawLine(0,Int_Ligne_Horizontal_1,400,Int_Ligne_Horizontal_2);
  105.     Int_Ligne_Horizontal_1 = Int_Ligne_Horizontal_1 + 10;
  106.     Int_Ligne_Horizontal_2 = Int_Ligne_Horizontal_2 + 10;
  107.     Int_Ligne_Horizontal_Fin = Int_Ligne_Horizontal_Fin + 1;
  108.    }
  109.    while(Int_Ligne_Vertical_Fin <=40)
  110.    {
  111.     g.setColor(Color.lightGray);
  112.     g.drawLine(Int_Ligne_Vertical_1,0,Int_Ligne_Vertical_2,400);
  113.     Int_Ligne_Vertical_1 = Int_Ligne_Vertical_1 + 10;
  114.     Int_Ligne_Vertical_2 = Int_Ligne_Vertical_2 + 10;
  115.     Int_Ligne_Vertical_Fin = Int_Ligne_Vertical_Fin + 1;
  116.    }
  117.   g.setColor(Color.black);
  118.   // On trace le repère
  119.   g.drawLine(0,200,400,200);
  120.   g.drawLine(200,0,200,400);
  121.   }
  122.   }
  123. }


 
 
2eme fichier
 

Code :
  1. /*
  2. * Main.java
  3. *
  4. * Created on 10 décembre 2005, 00:52
  5. *
  6. * To change this template, choose Tools | Options and locate the template under
  7. * the Source Creation and Management node. Right-click the template and choose
  8. * Open. You can then make changes to the template in the Source Editor.
  9. */
  10. package tracecourbe;
  11. /**
  12. *
  13. * @author Sai
  14. */
  15. public class Main {
  16.    
  17.     /** Creates a new instance of Main */
  18.     public Main() {
  19.     }
  20.     public static void main(String[] args){
  21.     FenetrePrin f = new FenetrePrin();
  22.    }
  23.    
  24. }


 
 
 
desolé pour le peu de commentaires, il est un peu tard :p


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15417
Memphis
Franc Archer
Profil : Habitué
Posté le 10-12-2005 à 11:39:10  
 

Oua, ca va t'as pas que les bases en java toi!
 
sinon c' est parfait mais malheureusement, tu utilises des trucs que je n'ai pas encore vu en cours, donc ca va paraître louche si je sors ca en tp, et puis en plus je ne dois pas le faire en applet.
 
sinon c' est vachement bien ton code, je vais essayer de voire comment tu as fait, et je vais essayer de m'en inspirer avec le peu d'outil doint je dispose.
 
merci à toi de t' être penché sur mon problème!

n°15418
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 12:27:04  
 

Rah mience, ok ben bon courage quand même alors :D


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15419
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 12:28:00  
 

Tiens pour un peu d'aide : http://java.developpez.com/faq/java/


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15421
Memphis
Franc Archer
Profil : Habitué
Posté le 10-12-2005 à 12:50:58  
 

merci pour ton lien, et surtout ne croit pas que ce que tu as fait n' a servi a rien, je vais me pencher dessus pour essayer  de comprendre, reflechir voir si je peux pas l'adapter a mon niveau, ....
merci vraiment, c' est sympa!

n°15422
Tastito
So lost in love
Profil : Dinosaure
Posté le 10-12-2005 à 12:55:30  
 

Oki si t'a besoin d'explication -> mp  
Bon courage :jap:


---------------
"Oh, that girl, you know she really, blows my, head apart,  
And the sky and the stars, all, synkronized...  
Eh no.... No no no, now me and her,  
We are in love... "  
n°15427
Memphis
Franc Archer
Profil : Habitué
Posté le 10-12-2005 à 19:04:03  
 

oki ca amrche, je te remercie!

n°15680
Memphis
Franc Archer
Profil : Habitué
Posté le 11-01-2006 à 17:26:39  
 

Salut all, j'avais oublié de donner la réponse:: la voici la voilà:
 

Code :
  1. public class func_exp {
  2. /**
  3.  * @param args
  4.  */
  5. public static void main(String[] args) {
  6.  // TODO Auto-generated method stub
  7.  final int TAILLE_CALCUL = 100;
  8.  int t,precision,pos,i;
  9.  int nb_car = 30;  /* nb de caractères pour l'affichage
  10.         des valeur positives.
  11.         l'etendue de a zone d'affichage représente
  12.         au total un espace de (nb_car*2)+1 caractères
  13.      */
  14.  double y[] = new double [  TAILLE_CALCUL];  // tableau des y
  15.         double max = -10000;
  16.         precision = 2; // détermine le pas de calcul : 1/precision
  17.  for (t=0;t <  TAILLE_CALCUL ; t++){
  18.      /*calcul de la valeur de y*/
  19.      y[t] = Math.exp(-0.1 * (float)t/precision) * Math.sin(0.5*(float)t/precision);
  20.     
  21.      /* sauvegarde de la valeur maximum en valeur asolue
  22.         pour pouvoir normaliser l'affichage */
  23.      if (max < Math.abs(y[t])) {
  24.   ind_max = t;
  25.   max = Math.abs(y[t]);
  26.      }
  27.        
  28.  }
  29.  /* calcul de la position de l'astérisque à l'écran */
  30.  for (t=0;t <  TAILLE_CALCUL ; t++){
  31.      pos =  (int) (y[t]/max * nb_car);
  32.      pos = pos+nb_car; //ajustement en fonction du signe de pos
  33.      /*  affichage des espaces*/
  34.      for (i=0;i<pos;i++)
  35.   System.out.print(" " );
  36.     
  37.      /* affichage de l'astérisque */
  38.      System.out.println("*" );
  39.  }
  40.    
  41.   }
  42. }

n°15681
Memphis
Franc Archer
Profil : Habitué
Posté le 11-01-2006 à 17:27:22  
 

Il est pas si costaud que ca, seulement 57 lignes, et encore y'a de l'espace!

  FORUM Rue-Montgallet.com
  Programmation
  Java

  Programme java fonction

© 2000-2024 Forum.rue-montgallet.com - Tous droits réservés