JAVA help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: JAVA help

Hybrid View

  1. #1
    Munchkin Guest

    JAVA help


    Hello,

    I have to do this problem and I can't get it to work correctly. There is
    something wrong with the display.
    I have to have it display on the applet string "this number is greater" or
    "numbers are equal" Here is the
    source code.
    If anyone can help, I would be extremely appreciative. I have been working
    on this for almost a week and
    I am ready to pull out my hair.

    import java.awt.Graphics; //import class Graphics
    import javax.swing.*; //import package javax.swing

    public class FloatingApplet extends JApplet {

    double sum;
    public void init ()
    {
    String firstNumber, //first string number
    secondNumber, //second string number
    result; //result string number

    double number1, //first number to double
    number2; //second number to double

    //read in first number from user
    firstNumber =
    JOptionPane.showInputDialog (
    "Enter first floating-point value");

    secondNumber =
    JOptionPane.showInputDialog (
    "Enter second floating-point value");

    //convert numbers from type string to type double
    number1 = Double.parseDouble (firstNumber);
    number2 = Double.parseDouble (secondNumber);

    //initialize result to empty string
    result = "";

    if ( number1 > number2 )
    result = result + number1 + ">" + number2 ;

    if (number1 < number2)
    result = result + number1 + "<" + number2 ;

    if (number1 == number2)
    result = result + number1 + " == " + number2
    ;


    }
    public void paint ( Graphics g)
    //draw the results with g.drawstring
    {
    g.drawRect (15, 10, 270, 20);
    g.drawString ( "" , 25,25 );

    }
    }



  2. #2
    John jmaclatchie Guest

    Re: JAVA help


    Suggest you replace:

    //convert numbers from type string to type double
    number1 = Double.parseDouble (firstNumber);
    number2 = Double.parseDouble (secondNumber);

    with this:

    //convert numbers from type string to type double
    Double one = new Double(firstNumber);
    Double two = new Double(secondNumber);
    number1 = one.doubleValue();
    number2 = two.doubleValue();

    and try it.
    ...John

    "Munchkin" <tjhubal@iex.net> wrote:
    >
    >Hello,
    >
    >I have to do this problem and I can't get it to work correctly. There is
    >something wrong with the display.
    >I have to have it display on the applet string "this number is greater"

    or
    >"numbers are equal" Here is the
    >source code.
    >If anyone can help, I would be extremely appreciative. I have been working
    >on this for almost a week and
    >I am ready to pull out my hair.
    >
    >import java.awt.Graphics; //import class Graphics
    >import javax.swing.*; //import package javax.swing
    >
    >public class FloatingApplet extends JApplet {
    >
    > double sum;
    > public void init ()
    > {
    > String firstNumber, //first string number
    > secondNumber, //second string number
    > result; //result string number
    >
    > double number1, //first number to double
    > number2; //second number to double
    >
    > //read in first number from user
    > firstNumber =
    > JOptionPane.showInputDialog (
    > "Enter first floating-point value");
    >
    > secondNumber =
    > JOptionPane.showInputDialog (
    > "Enter second floating-point value");
    >
    > //convert numbers from type string to type double
    > number1 = Double.parseDouble (firstNumber);
    > number2 = Double.parseDouble (secondNumber);
    >
    > //initialize result to empty string
    > result = "";
    >
    > if ( number1 > number2 )
    > result = result + number1 + ">" + number2 ;
    >
    > if (number1 < number2)
    > result = result + number1 + "<" + number2 ;
    >
    > if (number1 == number2)
    > result = result + number1 + " == " + number2
    >;
    >
    >
    > }
    > public void paint ( Graphics g)
    > //draw the results with g.drawstring
    > {
    > g.drawRect (15, 10, 270, 20);
    > g.drawString ( "" , 25,25 );
    >
    > }
    >}
    >
    >



  3. #3
    aniljoo Guest

    Re: JAVA help


    "Munchkin" <tjhubal@iex.net> wrote:
    >
    >Hello,
    >
    >I have to do this problem and I can't get it to work correctly. There is
    >something wrong with the display.
    >I have to have it display on the applet string "this number is greater"

    or
    >"numbers are equal" Here is the
    >source code.
    >If anyone can help, I would be extremely appreciative. I have been working
    >on this for almost a week and
    >I am ready to pull out my hair.
    >
    >import java.awt.Graphics; //import class Graphics
    >import javax.swing.*; //import package javax.swing
    >
    >public class FloatingApplet extends JApplet {
    >
    > double sum;
    > public void init ()
    > {
    > String firstNumber, //first string number
    > secondNumber, //second string number
    > result; //result string number
    >
    > double number1, //first number to double
    > number2; //second number to double
    >
    > //read in first number from user
    > firstNumber =
    > JOptionPane.showInputDialog (
    > "Enter first floating-point value");
    >
    > secondNumber =
    > JOptionPane.showInputDialog (
    > "Enter second floating-point value");
    >
    > //convert numbers from type string to type double
    > number1 = Double.parseDouble (firstNumber);
    > number2 = Double.parseDouble (secondNumber);
    >
    > //initialize result to empty string
    > result = "";
    >
    > if ( number1 > number2 )
    > result = result + number1 + ">" + number2 ;
    >
    > if (number1 < number2)
    > result = result + number1 + "<" + number2 ;
    >
    > if (number1 == number2)
    > result = result + number1 + " == " + number2
    >;
    >
    >
    > }
    > public void paint ( Graphics g)
    > //draw the results with g.drawstring
    > {
    > g.drawRect (15, 10, 270, 20);
    > g.drawString ( "" , 25,25 );
    >
    > }
    >}
    >
    >


    hai

    from what i understand from the above is that you

    want to print a string "greater or equalto.. in JApplet

    use g.drawString(stringname,x,y);
    hope this will help


  4. #4
    shawn Guest

    Re: Java Help


    I like to put in a "single student average" and a "remove student" and
    "three courses for each student" into my program, I have 96% done, been
    screwing up my program trying all these times.

    Need help please.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    public class StudentGrades extends JFrame {

    private JLabel name, id, grade, classAverage, highScore, scoreId,
    score, bs, studentList, sorted, explain, removelist;
    private JTextField nameText, idText, gradeText, highScoreText,
    removeText, classAveText, idScoreText, scoreText, bsText;
    private JTextArea listText, sortText;
    private JButton enterButton, averageButton, highScoreButton,
    removeButton, bsButton, sortButton ;

    /**used to keep track of the current number of students added
    */

    private int scount=0;

    /**Creates an array of Students (0 through 16).
    */

    private Student[] Students = new Student[16];
    class Student {
    private String name;
    private int id;
    private double grade;

    /** Gives assignments for array variables of class Student
    */
    public Student( String n, int d, double g )
    {
    name = n;
    id = d;
    grade = g;
    }
    /** getName method returns the 'name' in the Student array.
    */
    public String getName() { return name; }

    /** getId method returns the 'id' in the Student array.
    */

    public int getId() { return id; }

    /** getGrade method returns the 'grade' in the Student array.
    */

    public double getGrade() { return grade;}
    }

    /**Method for developing the GUI outline.
    */

    public StudentGrades()
    {
    super( "Student Grades - By: Shawn McNaughton" );//Display title
    of program in title bar.

    /**Bring in the container
    */

    Container c = getContentPane();

    /**Define a new JPanel for Name label and text field.
    */

    JPanel namePanel = new JPanel();
    namePanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    name = new JLabel("Name");
    name.setFont(new Font("Sans Serif", Font.BOLD, 13));
    namePanel.add(name);
    nameText = new JTextField(10);
    namePanel.add(nameText);

    /**Define a new JPanel for Id label and text field.
    */

    JPanel idPanel = new JPanel();
    idPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    id = new JLabel("ID");
    id.setFont(new Font("Sans Serif", Font.BOLD, 13));
    idPanel.add(id);
    idText = new JTextField(10);
    idPanel.add(idText);

    /**Define a new JPanel for Grade label and text field.
    */

    JPanel gradePanel = new JPanel();
    gradePanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    grade = new JLabel("Grade");
    grade.setFont(new Font("Sans Serif", Font.BOLD, 13));
    gradePanel.add(grade);
    gradeText = new JTextField(10);
    gradePanel.add(gradeText);

    /**Define a new JPanel for class Average label and text field.
    */

    JPanel classAveragePanel = new JPanel();
    classAveragePanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    classAverage = new JLabel("Class Average");
    classAverage.setFont(new Font("Sans Serif", Font.BOLD, 13));
    classAveragePanel.add(classAverage);
    classAveText = new JTextField(10);
    classAveragePanel.add(classAveText);

    /**Define a new JPanel for High Score label.
    */

    JPanel highScorePanel = new JPanel();
    highScorePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    highScore = new JLabel("High Score");
    highScore.setFont(new Font("Sans Serif", Font.BOLD, 13));
    highScorePanel.add(highScore);

    /**Define a new JPanel for ID label and text field.
    */

    JPanel scoreIdPanel = new JPanel();
    scoreIdPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    scoreId = new JLabel("ID");
    scoreId.setFont(new Font("Sans Serif", Font.BOLD, 13));
    scoreIdPanel.add(scoreId);
    idScoreText = new JTextField(10);
    scoreIdPanel.add(idScoreText);

    /**Define a new JPanel for Score label and text field.
    */

    JPanel scorePanel = new JPanel();
    scorePanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    score = new JLabel("Score");
    score.setFont(new Font("Sans Serif", Font.BOLD, 13));
    scorePanel.add(score);
    scoreText = new JTextField(10);
    scorePanel.add(scoreText);


    /**Define a new JPanel for B's label and text field.
    */

    JPanel bsPanel = new JPanel();
    bsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    bs = new JLabel("B's");
    bs.setFont(new Font("Sans Serif", Font.BOLD, 13));
    bsText = new JTextField(10);
    bsPanel.add(bs);
    bsPanel.add(bsText);

    /**Define a new JPanel for Student ID List and ID's Sorted labels.
    */

    JPanel listTitlePanel = new JPanel();
    listTitlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    studentList = new JLabel("Student ID List");
    studentList.setFont(new Font("Sans Serif", Font.BOLD, 13));
    sorted = new JLabel(" ID's Sorted");
    sorted.setFont(new Font("Sans Serif", Font.BOLD, 13));
    listTitlePanel.add(studentList);
    listTitlePanel.add(sorted);

    /**Define a new JPanel for Student ID List and ID's Sorted text
    *areas.
    */

    JPanel listTextPanel = new JPanel();
    listTextPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    listText = new JTextArea(15, 5);
    listText.setFont(new Font("Sans Serif", Font.BOLD, 13));
    sortText = new JTextArea(15, 5);
    sortText.setFont(new Font("Sans Serif", Font.BOLD, 13));
    listTextPanel.add(listText);
    listTextPanel.add(sortText);

    /**Define a main JPanel to hold all the other panels with the
    *BoxLayout Manager
    */

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    /**Add each of the above defined JPanels to the mainPanel
    */

    mainPanel.add(namePanel);
    mainPanel.add(idPanel);
    mainPanel.add(gradePanel);
    mainPanel.add(classAveragePanel);
    mainPanel.add(highScorePanel);
    mainPanel.add(scoreIdPanel);
    mainPanel.add(scorePanel);
    mainPanel.add(bsPanel);
    mainPanel.add(listTitlePanel);
    mainPanel.add(listTextPanel);
    c.add(mainPanel, BorderLayout.WEST); //add the main panel.

    /**Create a separate JPanel for the JButtons
    */

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
    enterButton = new JButton ("ADD STUDENT");
    removeButton = new JButton ("REMOVE STUDENT");
    averageButton = new JButton ("AVERAGE");
    highScoreButton = new JButton ("HIGH SCORE");
    bsButton = new JButton ("DISPLAY B's");
    sortButton = new JButton ("SORT ID's");
    explain = new JLabel ("Press until all sorted.");
    c.add(buttonPanel); //add the button panel.

    /**Add the Buttons to the button panel.
    */

    buttonPanel.add(removeButton);
    buttonPanel.add(enterButton);
    buttonPanel.add(highScoreButton);
    buttonPanel.add(bsButton);
    buttonPanel.add(averageButton);
    buttonPanel.add(sortButton);
    buttonPanel.add(explain);

    /**Event Handler (ActionListener) for the Enter Button. Runs the
    addstudent and
    *addIdList methods.
    */

    enterButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    addstudent();
    addIdList();

    }
    }
    );

    /**Event Handler (ActionListener) for Average Button. Calls calc_ave
    method.
    */

    averageButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    calc_avg();
    }
    }
    );

    /**Event Handler (ActionListener) for High Score Button. Calls calc_hs
    method.
    */

    highScoreButton.addActionListener(new ActionListener()

    {
    public void actionPerformed(ActionEvent e)

    {
    calc_hs();
    }
    }
    );

    /**Event Handler (ActionListener) for B's Button. Calls calc_bs
    method.
    */

    bsButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    calc_bs();
    }
    }
    );

    /**Event Handler (ActionListener) for the Sort Button. Calls
    sortIdArray method.
    */

    sortButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    sortIdArray();
    }
    }
    );



    /**Set size of container and call show method.
    */

    setSize( 400, 550 );
    show();
    }

    /**addstudent method that adds a student to the array.
    */

    private void addstudent()
    {
    String name,tempid,tempgrade;
    int id;
    double grade;
    if (scount < 17)
    {
    //Gets the text from the text Fields
    name = nameText.getText();
    tempid = idText.getText();
    tempgrade = gradeText.getText();
    if (tempid.length() != 0 && tempgrade.length() != 0
    && name.length() != 0)
    {
    //makes the actual student class inside of
    the array
    Students[scount] = new
    Student(name,Integer.parseInt(tempid),Float.parseFloat(tempgrade));

    //Integer.parseInt() takes a string
    value and returns an Integer.
    nameText.setText(""); //resets the
    text fields back to blank.
    idText.setText("");
    gradeText.setText("");
    nameText.requestFocus();//Sets the
    cursor to the name field
    scount++;//adds one to the student
    counter
    }
    }
    }

    /** addIdList method that adds the student ID's to the listText Text
    Area
    */

    private void addIdList()
    {
    byte i = 0;
    String tempStr = new String();
    for (i=0; i < scount; i++)
    {
    tempStr += Students[i].getId() + "\n";//gets the id via
    the getId method for array and assigns to tempStr
    }
    listText.setText(tempStr);
    }

    /**Sort array of id's and print out to sortText Text Area.
    */

    private void sortIdArray()
    {
    sortText.setText("");
    byte i = 0;
    String tempStr = new String();
    Student tempStudent;
    for (i = 0; i < scount-1; i++)
    {
    if (Students[i].getId() > Students[i+1].getId())
    {
    tempStudent = Students[i+1];
    Students[i+1] = Students[i];
    Students[i] = tempStudent;
    }
    }
    for (i=0; i < scount; i++)
    {
    tempStr += Students[i].getId() + "\n";
    }

    sortText.setText(tempStr);
    }

    /**Calculates all the letter grade
    */

    private void calc_bs()
    {
    int bcount=0;
    int ccount=0;
    int dcount=0;
    int acount=0;
    int fcount=0;
    for(int i=0;i<scount;i++)
    { if (Students[i].getGrade() >90 && Students[i].getGrade() <=
    100){acount ++;
    } else if (Students[i].getGrade() <80 &&
    Students[i].getGrade() >= 89.99){bcount ++;
    } else if(Students[i].getGrade() <70 &&
    Students[i].getGrade() >= 79.99){ccount ++;
    } else if(Students[i].getGrade() <60 &&
    Students[i].getGrade() >= 69.99){dcount ++;
    }else {fcount ++;}

    }

    //makes a new Integer for a sec with the bcount value, converts it to
    a string, and then sets
    //the text to it

    bsText.setText(new Integer(bcount).toString());
    }

    /**Finds the High Score
    */

    private void calc_hs()
    {
    String stuname = "";
    int stuid = 0;
    double stugrade = 0, bgrade = 0;
    for (int i = 0;i < scount;i++)
    {
    //Compares to see if the current students grade is
    greater then the last best grade, if it is
    //records the name, id and grade for later use.

    if (bgrade < Students[i].getGrade())
    {
    stuname = Students[i].getName();
    stuid = Students[i].getId();
    stugrade = Students[i].getGrade();
    bgrade = stugrade;
    }
    }

    //Changes the text fields to the values recorded earlier
    highScore.setText("High Score : " + stuname);

    //Changes highscore label and appends the name of the
    student with high score to label.
    idScoreText.setText(new Integer(stuid).toString());
    scoreText.setText(new Double(stugrade).toString());
    }

    /**Calculates the average
    */

    private void calc_avg()
    {
    double totalGrades = 0,studentGrade = 0;
    double averageGrades;
    for (int i=0;i<scount;i++)
    {
    studentGrade = Students[i].getGrade();
    totalGrades += studentGrade;
    }
    averageGrades = ((totalGrades)/scount);
    classAveText.setText(new Double(averageGrades).toString());
    }

    /**Adds student ID's to listText Text Area
    */

    private void addidlist()
    {
    //String stuname = "";
    int stuid = 0;
    for (int i = 0;i < scount;i++)
    {
    stuid = Students[i].getId();
    }
    listText.setText(new Integer(stuid).toString());
    }

    /**main method
    */

    public static void main( String args[] )
    {
    StudentGrades app = new StudentGrades();
    app.addWindowListener (
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {System.exit(0);
    }
    });
    }
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center