-1

If the user choose a class from the one's listed in LabCourse (BIO, CIS...) it should give the total plus lab charge (50), but my code is always executing total as 0 and printing the "This class don't have lab" message

What could the problem be?

CollegeCourse.java

public class CollegeCourse
{
String name;
int num;
int crd;
double fee;
double total;

public CollegeCourse(String n)
  {
  name =n;
  }

public double computetotal(double fee, int crd)

    {
          fee = fee * crd;
          System.out.println("Total is $" + fee);
          this.fee=fee;
          return fee;
    }

public String getname()
  {
       return name;
  }

public int getnum()
  {
       return num;
  }

public int getcrd()
  {
       return crd;
  }
    // public double getfee()
  // {
  //       return fee;
  // }
}

LabCourse.java

public class LabCourse extends CollegeCourse
{
 double total=0;

public LabCourse(String name, double fee)
{
  super(name);
}

public void computetotal(String name)
{
   super.computetotal(fee, crd);
   if((name == "BIO") || (name == "CHM") || (name == "CIS") || (name =="PHY"))
   {
      total = fee+ 50;
      System.out.println("Total with lab is: " + total);
   }
   else 
      System.out.println("This class don't have Lab");
}
}

UseCourse.java

import javax.swing.*;
import java.util.*;
import java.util.Scanner;
public class UseCourse
{
  public static void main(String args[]) throws Exception
  {
   String name;
   int num;
   int crd;
   double fee;

   Scanner inputDevice = new Scanner(System.in);

   System.out.print("Enter Department name: ");
   name = inputDevice.next();
   System.out.print("Enter Course number: ");
   num= inputDevice.nextInt();
   System.out.print("Enter Credit hours: ");
   crd = inputDevice.nextInt();
   System.out.print("Enter fee: ");
   fee = inputDevice.nextDouble();
   System.out.print("\n\n"); 

   CollegeCourse course = new CollegeCourse(name);
   course.computetotal(fee, crd);

   LabCourse full = new LabCourse(name, fee);
   full.computetotal(name);
   }
 } 
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Zack J
  • 3
  • 1
  • 2

2 Answers2

1

Strings are classes. You should not use == to compare classes (unless you want to check whether they are the exact same object (not just, in this case, the same text))

Some IDEs (e.g. NetBeans) will give you a warning when you try to use == on strings.

Try System.out.println(new String("ABC") == "ABC"); - this will print false.

Use equals instead:

if (name.equals("BIO") || name.equals("CHM") ||
    name.equals("CIS") || name.equals("PHY"))

A simpler option with regular expressions:

if (name.matches("BIO|CHM|CIS|PHY"))

Reference.

EDIT:

Another problem is that you're never setting up LabCourse.fee, but you use it here:

super.computetotal(fee, crd);

So set it up in the constructor:

public LabCourse(String name, double fee)
{
  super(name);
  this.fee = fee; // or add this as a parameter to CollegeCourse's constructor
}

But, since computetotal overwrites the fee, multiple calls to it may not quite work as you want, so you may want to pass it as a parameter to computetotal instead:

public class LabCourse extends CollegeCourse
{
  ...
  public void computetotal(String name, double fee)
  ...
}

Which leads to the question - does it make sense to have fee be a class variable? Or maybe it would make more sense to just put the code from computetotal in the constructor and get rid of computetotal or something along those lines. The same for name.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

I am not sure, but where exactly do you set the fee of the labcourse? And yeah, comparing strings must be done with String.equals() and not with ==.

Java comparison with == of two strings is false?

Community
  • 1
  • 1
darxsys
  • 1,560
  • 4
  • 20
  • 34