www.webdeveloper.com
Results 1 to 5 of 5

Thread: im back wit more questions

  1. #1
    Join Date
    Aug 2004
    Posts
    61

    im back wit more questions

    ok im having difficulty making this method work correctly. this is just a test to determine why its not doing my calculation right. in this class i have an incomeing string and int from another class called smester. its supposed to chekc wheather the string is "A" or not. and to return a calculation if it is true. but all i get is 1.0 cause of my else statment. so the method is not reading my incoming string right i think, not sure.
    does anyone in this forum know?

    thnks
    keke
    (both classes are listed below

    public class Courses
    {

    private int numCredits;
    private String grade;

    public void setGrade(String g)
    {

    grade = g;
    }
    public void setCredits(int c)
    {

    numCredits = c;
    }


    public double calcQualityPoints()
    {
    double points;
    double qualPoints;

    if(grade=="A")
    {

    points = 4.0;
    qualPoints = numCredits * points;

    return qualPoints;
    }
    else
    {
    return 1;
    }
    }

    }

    ////////////////////////////////////////////////////////////////////
    semester class
    ///////////////////////////////////////////////////////////////////

    import javax.swing.JOptionPane;

    public class Semester{

    public static void main(String[] args){

    int count = 1;
    int courses = Integer.parseInt(JOptionPane.showInputDialog("How many courses did you take htis semester"));


    while(count<=courses)
    {

    Courses school = new Courses();
    String grad = JOptionPane.showInputDialog("Whats was the letter grade for " + count + "class");
    school.setGrade(grad);

    int creds = Integer.parseInt(JOptionPane.showInputDialog("How Many Credits was your " + count + "class"));
    school.setCredits(creds);



    System.out.println(school.calcQualityPoints());

    count++;



    }




    }
    }

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    In Java, a String is an object, which means you cannot just use the equals operator for comparison. (this would compare the actual objects).

    You need to use the .equals(string) method.
    Code:
    public double calcQualityPoints()
    {
       double points = 4.0;
    
       if(grade.equals("A"))
          return (numCredits * points);
       return 1;
    }
    That should work for you.

    Regards.

  3. #3
    Join Date
    Aug 2004
    Posts
    61
    ok so that should work right, but i noticed u stored 4.0 in a variable, that means that i would have to write 6 different variables, correct? 1 for a, b ,b+,c,c+,d,d+,f,w. and the i would have to write all those into a nested if statment, so thats why i did it the wya i did. is therr another way of tackling this problem?

  4. #4
    Join Date
    Aug 2004
    Posts
    61
    thnks, works great, dont mind the previous reply.
    did u start ur company by urself? or did u have a group to help u out?

    thnks again
    keko
    Last edited by keko4201; 11-30-2004 at 08:30 PM.

  5. #5
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    You cna just change the value of the variable.
    Code:
    double points = 4.0;
    ...
    points = 3.0;
    In reference to the last question, I started the company on my own. Its currently on hold as im too busy with other parts of my life at the moment.

    Regards.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles