Java Subclass Method for Cashier and Clerk: Solving a Competition Challenge

  • Comp Sci
  • Thread starter Robben
  • Start date
  • Tags
    Java Method
In summary: Math.PI * radius * radius; }}class Rectangle implements Shape { private double length; // length of one side private double width; // length of the other side Rectangle (double length, double width) {...}}// FindAreasOfShapes.javapublic class FindAreasOfShapes { public static void main (String[] args) { Shape circle = new Circle (2.0);
  • #1
Robben
166
2

Homework Statement



Suppose I have a subclass called, Cashier, and a super class named, Clerk. How do I make a method that is called on a Cashier object and takes in an instance of Cashier? What method has a structure like this?

Homework Equations



None

The Attempt at a Solution



This method is going to place a competition between the subclass Cashier and the superclass Clerk and returns Cashier member that won the competition.

I have my particular competition created, by creating a helper method but I just don't know how to apply the problem statement above.

Code:
public class Cashier {
...
}

public class Clerk extends Cashier {
...
}
 
Physics news on Phys.org
  • #2
The first problem is you said the super class (aka parent class should be Clerk) and the subclass (aka child class) should be Cashier.

However you've written them the other way around. Basically in your code example you've said a Clerk is a kind of Cashier.

In contrast, my code says a Cashier is a kind of Clerk

Java:
public class Clerk {
...
}

public class Cashier extends Clerk {
...
    public boolean isWinner(Cashier employee) {
        boolean winStatus = false;
        ... code to set winStatus as true or false...
        return winStatus;
    }
...
}

The above code shows the isWinner() method take in as input the Cashier employee and return a boolean value.

Is that what you mean?
 
Last edited:
  • #3
jedishrfu said:
The first problem is you said the super class (aka parent class should be Clerk) and the subclass (aka child class) should be Cashier.

However you've written them the other way around. Basically in your code example you've said a Clerk is a kind of Cashier.

In contrast, my code says a Cashier is a kind of Clerk

Java:
public class Clerk {
...
}

public class Cashier extends Clerk {
...
    public boolean isWinner(Cashier employee) {
        boolean winStatus = false;
        ... code to set winStatus as true or false...
        return winStatus;
    }
...
}

The above code shows the isWinner() method take in as input the Cashier employee and return a boolean value.

Is that what you mean?

Opps, on the first part. You're right, Cashier should be a kind of clerk.

Hm, what does it mean to make a method that is called on a subclass object and takes in an instance of Subclass? Also, I was told that, how can I handle the case where the object that the method is called on needs to be returned. Which doesn't really make much sense to me. Is there some type of keyword that refers to the current object being acted upon?
 
  • #5
jedishrfu said:
Do you mean like the 'this' instance?

That's the first thing that popped into my head but I am not sure how that "this" applies to the question that was asked.
 
  • #6
I'm not sure how I can help you without giving you the answer but here's the Shapes example that may give you some clues about how java
can be used.

Java:
public interface Shape { // interface declaration
    double area ();
}

class Circle extends Shape { // class declaration

    private double radius; // instance variable

    Circle (double radius) { // constructor
        this.radius = radius;
    }

    public double area () { // dynamic method
        return Math.PI * radius * radius;
    }
}

class Rectangle implements Shape {

    private double length; // length of one side
    private double width; // length of the other side

    Rectangle (double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double area () {
        return length * width;
    }
}

// FindAreasOfShapes.java

public class FindAreasOfShapes {
    public static void main (String[] args) {

        Shape circle = new Circle (2.0);
        Shape rect = new Rectangle (3.0, 5.0);

        System.out.println (circle.area());
        System.out.println (rect.area());
    }
}
 
  • #7
jedishrfu said:
I'm not sure how I can help you without giving you the answer but here's the Shapes example that may give you some clues about how java
can be used.

Java:
public interface Shape { // interface declaration
    double area ();
}

class Circle extends Shape { // class declaration

    private double radius; // instance variable

    Circle (double radius) { // constructor
        this.radius = radius;
    }

    public double area () { // dynamic method
        return Math.PI * radius * radius;
    }
}

class Rectangle implements Shape {

    private double length; // length of one side
    private double width; // length of the other side

    Rectangle (double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double area () {
        return length * width;
    }
}

// FindAreasOfShapes.java

public class FindAreasOfShapes {
    public static void main (String[] args) {

        Shape circle = new Circle (2.0);
        Shape rect = new Rectangle (3.0, 5.0);

        System.out.println (circle.area());
        System.out.println (rect.area());
    }
}

THank you very much!
 
  • #8
How is it helpful?

The key notion in the code is that you can use shape to reference the objects even though they are actually Circles and Rectangles. The Shape interface could also be defined as a super class and its subclasses would then need to override the area method.
 
  • #9
jedishrfu said:
How is it helpful?

Lol, I thought you said that you couldn't help any further so I was thanking you for your time.

The key notion in the code is that you can use shape to reference the objects even though they are actually Circles and Rectangles. The Shape interface could also be defined as a super class and its subclasses would then need to override the area method

I don't get the point in having the interface? If you are going to override it anyways in both scenarios then doesn't that make the interface pretty useless?
 
  • #10
You're missing the point of the interface. It defines a contract that says any class that wants to be like the interface MUST implements the methods the interface defines. It is used extensively in Java. The best example is the Runnable interface that should a class implement it must implement the run() method.

In concert with Runnable, the Thread class can then be used to create a thread that runs the run() method of the class. The Thread method only needs to know that the instance it's given implements Runnable and thus has a run() method that can be called.

Here's a great tutorial on Runnable and you might see uses in your own code

http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html
 
  • Like
Likes Robben
  • #11
While a little off track from your original post, I thought you might like to see this on why interfaces are so important

http://www.javaworld.com/article/2076841/core-java/designing-with-interfaces.html

The idea is that interfaces can help you tighten up and manage your program design better and even allow you to make your code "plug-able" ie swap one class out that implements your interface with another that does things differently but still supports the same interface. In a sense design with interfaces and organize your classes by interfaces.
 
  • #12
Thank you very much. I will be reading those links and also YouTubing it as well, to try and understand what exactly the problem wants me to do.
 

1. What is a Java method?

A Java method is a block of code that performs a specific task. It is a collection of statements that are grouped together and can be called upon to perform that task whenever needed.

2. How do you declare a Java method?

To declare a Java method, you need to specify the access modifier (public, private, or protected), the return type (void or a specific data type), the name of the method, and any parameters it may take. For example:
public void methodName(int parameter1, String parameter2) { // method body }

3. What is the purpose of a subclass in Java?

A subclass in Java allows for the creation of a new class that inherits the properties and methods of an existing class. This enables code reuse, as the subclass can add its own unique features while still having access to the functionality of its superclass.

4. How do you create a subclass in Java?

To create a subclass in Java, you use the "extends" keyword in the class declaration, followed by the name of the superclass. For example:
public class Subclass extends Superclass { // subclass body }

5. Can a subclass override methods of its superclass?

Yes, a subclass can override methods of its superclass by redefining the method with the same name, return type, and parameters. This allows the subclass to modify or extend the behavior of the original method to suit its specific needs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
803
Back
Top