Hallmark Problem [Please help]

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 2590 Views - Last Post: 02 February 2013 - 12:13 PM Rate Topic: -----

#16 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:14 PM

Is this looking any better? I thought that you couldn't return a string without setting the return type to Object? I think I am close to getting this down :dontgetit:
import java.util.*;
public class Hallmark
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args)
  {
    Card userCard;
    boolean isInt = true;
    int iUserInput;
    int iInputCounter = 1;
    String sSender = toPerson();
    String sRecipient = fromPerson();
    
//menu
    String sMenu = "Please select your card:" + 
      "\n\t1) Christmas" + 
      "\n\t2) Valentines" +
      "\n\t3) Birthday" +
      "\n\t4) Get well soon" +
      "\n\t5) Anniversary" +
      "\n\t6) New Baby" +
      "\n\t7) Thank You" +
      "\n\t8) Congratulations" + 
      "\n\t9) Blank Card\n";
    
    //print menu
    System.out.print(sMenu);
 
    //confirm input
    do
    {
      System.out.println("Please enter a valid choice");
      try
      {
        iUserInput = console.nextInt();
        if ((iUserInput >= 0) && (iUserInput <= 9))
        {
          
          //counter
          iInputCounter--;
                 
          switch(iUserInput)
          {
    //christmas card
            case 1: userCard = new Christmas();
            break;
   
   //valentines card
            case 2: userCard = new Valentines();
            break;
            
   //birthday card
            case 3: userCard = new Birthday();
            break;
         
   //getwellsoon card
            case 4: userCard = new GetWellSoon();
            break;
            
   //anniversary card
            case 5: userCard = new Anniversary();
            break;
            
   //newbaby card
            case 6: userCard= new NewBaby();
            break;
            
   //thankyou card
            case 7: userCard = new ThankYou();
            break;
            
   //congratulations card
            case 8: userCard = new Congratulations();
            break;
            
   //blank card
            default: userCard  = new BlankCard();
            break;
          }
          //generic information
          userCard.make(sSender, sRecipient);
        }
        else
        {
          System.out.println("You have not entered a valid choice");
          continue;
        }
      }
      catch (InputMismatchException e)
      {
        System.out.println("You have entered an invalid input");
        console.next();
        continue;
      }
    }while(iInputCounter > 0);
  }
  public String toPerson()
  {
  System.out.println("Please enter your name");
  String sSender = console.nextLine();
  return sSender;
 }
 public String fromPerson()
 {
  System.out.println("Please enter recipients name");
  String sRecipient = console.nextLine();
  return sRecipient;
 }
}


Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:17 PM

That looks a lot better. Probably even works.
Was This Post Helpful? 0
  • +
  • -

#18 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:22 PM

First off I want to say thank you so much for the help you have given me on this difficult adventure for me. I can almost see the light only a few more issues... also I have actually been learning a ton from this (thank you again);
*change Valentines with whatever card you want
public class Valentines extends Card
{
  Birthday(String sSender, String sRecipient)
  {
    sRecipient = sRecipient;
    sSender = sFrom;
  }
  public void makeCard()
  {
    System.out.println("christmas");
  }
}



This is now giving me the error

Quote

1 error found:
File: G:\CISS111\Homework 2\Code\Valentines.java [line: 3]
Error: invalid method declaration; return type required


I tried switching it to void but then I realized that, would result in no value being returned would I want to return a String? Also, how the hello kitty would I go about making a semi-card like looking Sring to go along with this(a loop of some type)? Maybe my variables are wrong or something?

This post has been edited by k3y: 31 January 2013 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:24 PM

Well, you'd want that constructor to be a Valentines, not a Birthday. :)
Was This Post Helpful? 1
  • +
  • -

#20 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:33 PM

After making sure I had the right names on each of the constructors(rookie mistake...). I went to the Hallmark class and compiled it and recieved a number of errors... I am sure it's some noob mistake of mine

Quote

12 errors found:
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 11]
Error: non-static method toPerson() cannot be referenced from a static context
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 12]
Error: non-static method fromPerson() cannot be referenced from a static context
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 45]
Error: constructor Christmas in class Christmas cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 53]
Error: constructor Birthday in class Birthday cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 57]
Error: constructor GetWellSoon in class GetWellSoon cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 61]
Error: constructor Anniversary in class Anniversary cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 65]
Error: constructor NewBaby in class NewBaby cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 69]
Error: constructor ThankYou in class ThankYou cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 73]
Error: constructor Congratulations in class Congratulations cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 77]
Error: constructor BlankCard in class BlankCard cannot be applied to given types;
required: java.lang.String,java.lang.String
found: no arguments
reason: actual and formal argument lists differ in length
File: G:\CISS111\Homework 2\Code\Hallmark.java [line: 81]
Error: cannot find symbol
symbol: method make(java.lang.String,java.lang.String)
location: variable userCard of type Card
File: G:\CISS111\Homework 2\Code\Valentines.java [line: 6]
Error: cannot find symbol
symbol: variable sFrom
location: class Valentines

Hallmark/Main
import java.util.*;
public class Hallmark
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args)
  {
    Card userCard;
    boolean isInt = true;
    int iUserInput;
    int iInputCounter = 1;
    String sSender = toPerson();
    String sRecipient = fromPerson();
    
//menu
    String sMenu = "Please select your card:" + 
      "\n\t1) Christmas" + 
      "\n\t2) Valentines" +
      "\n\t3) Birthday" +
      "\n\t4) Get well soon" +
      "\n\t5) Anniversary" +
      "\n\t6) New Baby" +
      "\n\t7) Thank You" +
      "\n\t8) Congratulations" + 
      "\n\t9) Blank Card\n";
    
    //print menu
    System.out.print(sMenu);
 
    //confirm input
    do
    {
      System.out.println("Please enter a valid choice");
      try
      {
        iUserInput = console.nextInt();
        if ((iUserInput >= 0) && (iUserInput <= 9))
        {
          
          //counter
          iInputCounter--;
                 
          switch(iUserInput)
          {
    //christmas card
            case 1: userCard = new Christmas();
            break;
   
   //valentines card
            case 2: userCard = new Valentines();
            break;
            
   //birthday card
            case 3: userCard = new Birthday();
            break;
         
   //getwellsoon card
            case 4: userCard = new GetWellSoon();
            break;
            
   //anniversary card
            case 5: userCard = new Anniversary();
            break;
            
   //newbaby card
            case 6: userCard= new NewBaby();
            break;
            
   //thankyou card
            case 7: userCard = new ThankYou();
            break;
            
   //congratulations card
            case 8: userCard = new Congratulations();
            break;
            
   //blank card
            default: userCard  = new BlankCard();
            break;
          }
          //generic information
          userCard.make(sSender, sRecipient);
        }
        else
        {
          System.out.println("You have not entered a valid choice");
          continue;
        }
      }
      catch (InputMismatchException e)
      {
        System.out.println("You have entered an invalid input");
        console.next();
        continue;
      }
    }while(iInputCounter > 0);
  }
  public String toPerson()
  {
  System.out.println("Please enter your name");
  String sSender = console.nextLine();
  return sSender;
 }
 public String fromPerson()
 {
  System.out.println("Please enter recipients name");
  String sRecipient = console.nextLine();
  return sRecipient;
 }
}


Card
public abstract class Card
{
  String sRecipient;
  String sSender;
  public abstract void makeCard();
}


Sample card class
public class Christmas extends Card
{
  Christmas(String sSender, String sRecipient)
  {
    sRecipient = sRecipient;
    sSender = sSender;
  }
  public void makeCard()
  {
    System.out.println("christmas");
  }
}



Maybe I am assigning the wrong value here?
sRecipient = sRecipient;
    sSender = sSender;



Maybe I need to pass some value to the object?
  case 1: userCard = new Christmas(value?);
            break;


Also to show that you really have been helping me learn, after I frankenstein this code together, I will break it down and explain how it works to show that I truly do know what is happening(as best as I can :smartass:/> )

This post has been edited by k3y: 31 January 2013 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Hallmark Problem [Please help]

Posted 31 January 2013 - 02:45 PM

Ah, yes, of course - a static method can't call instance methods. Read up on static and instance and what they mean in Java and see if you can sort that one out.

The compiler output for the other errors is pretty clear, you can probably dope it out.
(I'll be glad to help out, but I'm going to be tied up from now until quite late tonight)
Was This Post Helpful? 0
  • +
  • -

#22 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 11:33 AM

I am so close (I think). Here is what I have so far
Hallmark/Main
import java.util.*;
public class Hallmark
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args)
  {
    Card userCard;
    boolean isInt = true;
    int iUserInput;
    int iInputCounter = 1;
    String sSender = toPerson();
    String sRecipient = fromPerson();
    
//menu
    String sMenu = "Please select your card:" + 
      "\n\t1) Christmas" + 
      "\n\t2) Valentines" +
      "\n\t3) Birthday" +
      "\n\t4) Get well soon" +
      "\n\t5) Anniversary" +
      "\n\t6) New Baby" +
      "\n\t7) Thank You" +
      "\n\t8) Congratulations" + 
      "\n\t9) Blank Card\n";
    
    //print menu
    System.out.print(sMenu);
 
    //confirm input
    do
    {
      System.out.println("Please enter a valid choice");
      try
      {
        iUserInput = console.nextInt();
        if ((iUserInput >= 0) && (iUserInput <= 9))
        {
          
          //counter
          iInputCounter--;
                 
          switch(iUserInput)
          {
    //christmas card
            case 1: userCard = new Christmas(sSender, sRecipient);
            break;
   
   //valentines card
            case 2: userCard = new Valentines(sSender, sRecipient);
            break;
            
   //birthday card
            case 3: userCard = new Birthday(sSender, sRecipient);
            break;
         
   //getwellsoon card
            case 4: userCard = new GetWellSoon(sSender, sRecipient);
            break;
            
   //anniversary card
            case 5: userCard = new Anniversary(sSender, sRecipient);
            break;
            
   //newbaby card
            case 6: userCard= new NewBaby(sSender, sRecipient);
            break;
            
   //thankyou card
            case 7: userCard = new ThankYou(sSender, sRecipient);
            break;
            
   //congratulations card
            case 8: userCard = new Congratulations(sSender, sRecipient);
            break;
            
   //blank card
            default: userCard  = new BlankCard(sSender, sRecipient);
            break;
          }
          //generic information
          userCard.makeCard();
        }
        else
        {
          System.out.println("You have not entered a valid choice");
          continue;
        }
      }
      catch (InputMismatchException e)
      {
        System.out.println("You have entered an invalid input");
        console.next();
        continue;
      }
    }while(iInputCounter > 0);
  }
  public static String toPerson()
  {
  System.out.println("Please enter your name");
  String sSender = console.nextLine();
  return sSender;
 }
 public static String fromPerson()
 {
  System.out.println("Please enter recipients name");
  String sRecipient = console.nextLine();
  return sRecipient;
 }
}



Card
public abstract class Card
{
  String sRecipient;
  String sSender;
  public abstract void makeCard();
}



Sample christmas card (replace w/ whatever)
public class Christmas extends Card
{
  Christmas(String sSender, String sRecipient)
  {
    sRecipient = sRecipient;
    sSender = sSender;
  }
  public void makeCard()
  {
    System.out.println("christmas");
    System.out.println(sRecipient);
    System.out.println(sSender);
  }
}



The code seems to run however; If I enter a christmas card it returns 2 null strings. I'm not sure where I am assigning the wrong values to the class, any ideas?

This post has been edited by k3y: 01 February 2013 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#23 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 02:50 PM

Also here is my valentine's day card class. I tested it out in a different program with a sample string and it works, however it doesn't seem to work and only returns a null.

Any hints, suggestions are welcome. Thank you.
public class Valentines extends Card
{
  Valentines(String sSender, String sRecipient)
  {
    sRecipient = sRecipient;
    sSender = sSender;
  }
  public void makeCard()
  {
    System.out.println("Happy Valentines Day <3!");
    System.out.println("to " + sRecipient);
    int iStringLength = sRecipient.length();
    for(int x = 0; x < iStringLength; x++)
    {
      if(x % 2 == 0)
      {
        System.out.print("x");
      }
      else
      {
        System.out.print("o");
      }
    }
    System.out.println("from" + sSender);
    System.out.println("to" + sRecipient);
  }
}


Maybe I need more parameters?

This post has been edited by k3y: 01 February 2013 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#24 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 03:06 PM

You are setting the local variables instead of the instance variables
The local variables will get garbage collected some time after the constructor has returned
public class Christmas extends Card
{
  Christmas(String sSender, String sRecipient) //Local variables are sSender and sRecipient
  {
    sRecipient = sRecipient; //Setting the variable to itself
    sSender = sSender; //Setting the variable to itself
    this.sRecipient = sRecipient; //Using the instance variable inherited from Card
    this.sSender = sSender; //Using the instance variable inherited from Card
  }
  public void makeCard()
  {
    System.out.println("christmas");
    System.out.println(sRecipient);
    System.out.println(sSender);
  }
}




Was This Post Helpful? 2
  • +
  • -

#25 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 03:12 PM

That did the trick. I now have valid to and froms. After I clean this up and make it look pretty, i'll post my final code. Aswell as my explanation to show that you have all helped alot!
Was This Post Helpful? 0
  • +
  • -

#26 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 04:08 PM

Okay, I have about 90% of this code completed. However; now I am unable to return the user generated message**. I tried my best to make the code as clean as possible (all the other cards are working, except the blank card).

/*
 * 1) Your program should have at least 9 classes in it that inherit from the abstract card class. (1 for each type of card) x
 * 2) Your program should asks the user who the card is for and who it is from.  x
 * 3) It should also ask them what type of card they would like to create, by giving the user a menu to choose from? x
 * 4) If it is a birthday card, it also asks how old the recipient is. x
 * 5) If it is a blank card then it asks what personal message the sender would like to include. x
 * 6) If it is a Valentine's day card it personalizes the card by adding XOs to the end of the card (hugs and kisses) based on the length of the recipient's name. x
 */
import java.util.*;
public class Hallmark
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args)
  {
// variable declaration
    boolean isInt = true;
    Card userCard;
    int iUserInput;
    int iInputCounter = 1;
    String sSender = toPerson();
    String sRecipient = fromPerson();
    
//menu, point 3
    String sMenu = "Please select your card:" + 
      "\n\t1) Christmas" + 
      "\n\t2) Valentines" +
      "\n\t3) Birthday" +
      "\n\t4) Get well soon" +
      "\n\t5) Anniversary" +
      "\n\t6) New Baby" +
      "\n\t7) Thank You" +
      "\n\t8) Congratulations" + 
      "\n\t9) Blank Card\n";
    
//print menu
    System.out.print(sMenu);
 
//confirm input
    do
    {
      System.out.println("Please enter a valid choice");
      try
      {
        iUserInput = console.nextInt();
        if ((iUserInput >= 0) && (iUserInput <= 9))
        {
//counter
          iInputCounter--;
                 
          switch(iUserInput)
          {
//christmas card, point 1
            case 1: 
              userCard = new Christmas(sSender, sRecipient);
              break;
              
//valentines card, point 1
            case 2: 
              userCard = new Valentines(sSender, sRecipient);
              break;
            
//birthday card, point 1
            case 3: 
              int iAge = agePerson();
              userCard = new Birthday(sSender, sRecipient, iAge);
              break;
         
//getwellsoon card, point 1
            case 4: 
              userCard = new GetWellSoon(sSender, sRecipient);
              break;
            
//anniversary card, point 1
            case 5: 
              userCard = new Anniversary(sSender, sRecipient);
              break;
            
//newbaby card, point 1
            case 6: 
              userCard= new NewBaby(sSender, sRecipient);
              break;
            
//thankyou card, point 1
            case 7: 
              userCard = new ThankYou(sSender, sRecipient);
              break;
            
//congratulations card, point 1
            case 8: 
              userCard = new Congratulations(sSender, sRecipient);
              break;
            
//blank card, point 1
            default:
              String sMessageToRecipient = sMessage();
              userCard = new BlankCard(sSender, sRecipient, sMessageToRecipient);
              break;
          }
          //generic information
          userCard.makeCard();
        }
        else
        {
          System.out.println("You have not entered a valid choice");
          continue;
        }
      }
      catch (InputMismatchException e)
      {
        System.out.println("You have entered an invalid input");
        console.next();
        continue;
      }
    }while(iInputCounter > 0);
  }
  //point 2
  public static String toPerson()
  {
    System.out.println("Please enter your name");
    String sSender = console.nextLine();
    return sSender;
  }
  
//point 2
  public static String fromPerson()
  {
    System.out.println("Please enter recipients name");
    String sRecipient = console.nextLine();
    return sRecipient;
  }
  
//point 4
  public static int agePerson()
  {
    System.out.println("Please enter recipients age");
    int iAge = console.nextInt();
    return iAge;
  }
  
//point 5
public static String sMessage()
  {
    System.out.println("Please enter your message to the recipient: ");
    String sMessageToRecipient = console.nextLine();
    return sMessageToRecipient;
  }
}


blankcard class
public class BlankCard extends Card
{
  BlankCard(String sSender, String sRecipient, String sMessageToRecipient)
  {
    this.sRecipient = sRecipient;
    this.sSender = sSender;
    this.sMessageToRecipient = sMessageToRecipient;
  }
  public void makeCard()
  {
    System.out.println("to " + sRecipient);
    System.out.print("\n\n\n\n");
    System.out.println("from: " + sSender);
    System.out.println("MESSAGE:\n" + sMessageToRecipient);
  }
}



Card class
public abstract class Card
{
  String sRecipient;
  String sSender;
  String sMessageToRecipient;
  int iAge;
  public abstract void makeCard();
}



Is it because String is technically not a primitive data type?

EDIT: Message ** not method

This post has been edited by k3y: 01 February 2013 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#27 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 04:14 PM

Quote

now I am unable to return the user generated method


What exactly are you talking about? You are not having a user generate a method (wtf), and you are certainly not returning a method anywhere ^^
Was This Post Helpful? 1
  • +
  • -

#28 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 04:19 PM

Oh wow I really miswrote that I meant that I can run the program no problem but if the user selects blank card. The prompt never appears for the user to enter a message.
Was This Post Helpful? 0
  • +
  • -

#29 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 05:11 PM

When you are selecting a menu option, you will enter something like the below in the console

Quote

9\n

It will read "9" when you do nextInt() but there is still a newline character to be read
Your next nextLine() will therefore return an empty String because it will read the "\n" that was left over in the buffer

This post has been edited by CasiOo: 01 February 2013 - 05:11 PM

Was This Post Helpful? 2
  • +
  • -

#30 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Hallmark Problem [Please help]

Posted 01 February 2013 - 05:28 PM

EDIT: I just called called console.nextLine() to remove the old value and then called the console for the message. It works now =D

This post has been edited by k3y: 01 February 2013 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3