The Artima Developer Community
Sponsored Link

Java Answers Forum
I need some help

2 replies on 1 page. Most recent reply: Jun 7, 2003 10:45 AM by Elad

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Elad

Posts: 2
Nickname: elad
Registered: Jun, 2003

I need some help Posted: Jun 7, 2003 1:20 AM
Reply to this message Reply
Advertisement
Hi
I know my question is probably way too simple and stupid, but I have to hand in this paper to my teacher by tomorrow...

It's an assignment about class and inheritance.
I wrote these classes for greeting cards:

 
public class GreetingCard {
	private String sender;
	private String recipient;	
 
	public GreetingCard(String recipient, String sender) {
		this.recipient = recipient;
		this.sender = sender;
	}
 
	public void setRecipient(String recipient) {
		this.recipient = recipient;
	}
 
	protected String greetingMsg() {
		return "Best Greetings!";
	}
 
	public String showCard() {
		return "Dear " + this.recipient + ", \n" + this.greetingMsg() + "\n" + this.sender;
	}	
}
 
 
public class BirthdayCard extends GreetingCard {
	private int age;	
 
	public BirthdayCard(String recipient, String sender, int age) {
		super(recipient, sender);
		this.age = age;
	}
 
	protected String greetingMsg() {
		return "Happy " + age + "th Birthday!";
	}	
}
 
 
public class WeddingCard extends GreetingCard {
	private String bride;
	private String groom;	
 
	public WeddingCard(String bride, String groom, String sender) {
		super(bride + " & " + groom, sender);
		this.bride = bride;
		this.groom = groom;
	}
 
	public void setBride(String bride) {
		this.bride = bride;
		super.setRecipient(this.bride + " & " +  this.groom);
	}
 
	public void setGroom(String groom) {
		this.groom = groom;
		super.setRecipient(this.bride + " & " +  this.groom);
	}
 
	protected String greetingMsg() {
		return "May you live happily ever after";
	}	
}
 
 
class AdultBirthday extends BirthdayCard{
	public AdultBirthday(String recipient, String sender, int age) {
		super(recipient, sender, age);
	}
 
	protected String greetingMsg() {
		return super.greetingMsg() + "\n" +"You haven't changed at all!!";
	}	
}
 
 
public class YouthBirthday extends BirthdayCard {
	public YouthBirthday(String recipient, String sender, int age) {
		super(recipient, sender, age);
	}
 
	protected String greetingMsg() {
		return super.greetingMsg() + "\n" +"How have you grown!!";
	}	
}


Now, I have to write a class called ManyCards, which must include an array of at least five different kinds of cards, and then I should write a loop that prints all the cards.
So I wrote this:

 
class ManyCards {
	public static void main(String[] args) {
		
		GreetingCard[] gCards = new GreetingCard[5];
		gCards[0] = new GreetingCard("Recipient", "Sender");
		gCards[1] = new BirthdayCard("Recipient", "Sender", 18);
		gCards[2] = new WeddingCard("Bride", "Groom", "Sender");
		gCards[3] = new YouthBirthday("Recipient", "Sender", 8);
		gCards[4] = new AdultBirthday("Recipient", "Sender", 30);
		
		for (int i=0; i <= 4; i++) {
			System.out.println(gCards[i].showCard()); }
	}	
}


But it's not good enough, because it has to do something with down-casting and up-casting, but I have no idea how to do it... :-(

Any help?

Cheers
Elad

(I apologize for my bad english, but please forgive me, english is not my native tongue)


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: I need some help Posted: Jun 7, 2003 8:11 AM
Reply to this message Reply
Your code looks pretty good to me.

Every object you created is an Object and GreetingCard object and maybe one or more extended type of GreetingCard.

You could make an array of Objects, with each object a completely different type of Object:
Object[] cards = {
 
     new GreetingCard("Recipient", "Sender"), 
     new BirthdayCard("Recipient", "Sender", 18),
     new WeddingCard("Bride", "Groom", "Sender"),
     new YouthBirthday("Recipient", "Sender", 8),
     new AdultBirthday("Recipient", "Sender", 30),
};


and then iterate as follows:

        for (int i = 0; i < cards.length; i++){
            GreetingCard nextCard = (GreetingCard)cards[i];
            System.out.println(nextCard.showCard());
        } 

You could iterate through your cards and write out a message about each card type:
        System.out.println("My " + cards.length + " cards include the following types:");
        for (int i = 0; i < cards.length; i++){
            GreetingCard nextCard = (GreetingCard)cards[i];
            if (nextCard instanceof GreetingCard) System.out.println("Card number " + (i + 1) + " is a Greeting Card");
            if (nextCard instanceof BirthdayCard) System.out.println("Card number " + (i + 1) + " is a Birthday Card");
            if (nextCard instanceof WeddingCard) System.out.println("Card number " + (i + 1) + " is a Wedding Card");
            if (nextCard instanceof AdultBirthday) System.out.println("Card number " + (i + 1) + " is a Adult Birthday Card");
            if (nextCard instanceof YouthBirthday) System.out.println("Card number " + (i + 1) + " is a Youth Birthday Card");
        }


The whole code:

class MyCards{
 
    public static void main(String[] args){
        Object[] cards = {
            new GreetingCard("Recipient", "Sender"), 
            new BirthdayCard("Recipient", "Sender", 18),
            new WeddingCard("Bride", "Groom", "Sender"),
            new YouthBirthday("Recipient", "Sender", 8),
            new AdultBirthday("Recipient", "Sender", 30),
        }; 
        for (int i = 0; i < cards.length; i++){
            GreetingCard nextCard = (GreetingCard)cards[i];
            System.out.println(nextCard.showCard());
        }
        System.out.println("My " + cards.length + " cards include the following types:");
        for (int i = 0; i < cards.length; i++){
            GreetingCard nextCard = (GreetingCard)cards[i];
            if (nextCard instanceof GreetingCard) System.out.println("Card number " + (i + 1) + " is a Greeting Card");
            if (nextCard instanceof BirthdayCard) System.out.println("Card number " + (i + 1) + " is a Birthday Card");
            if (nextCard instanceof WeddingCard) System.out.println("Card number " + (i + 1) + " is a Wedding Card");
            if (nextCard instanceof AdultBirthday) System.out.println("Card number " + (i + 1) + " is a Adult Birthday Card");
            if (nextCard instanceof YouthBirthday) System.out.println("Card number " + (i + 1) + " is a Youth Birthday Card");
        }
    }
}

Elad

Posts: 2
Nickname: elad
Registered: Jun, 2003

Thanks Posted: Jun 7, 2003 10:45 AM
Reply to this message Reply
Thanks Charles.
You saved my class mark!

Cheers,
Elad :-)

Flat View: This topic has 2 replies on 1 page
Topic: Java WebStart Previous Topic   Next Topic Topic: Help : How can I reload a  file .class ???

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use