Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,878 people online right now. Registration is fast and FREE... Join Now!




homework help

 
Reply to this topicStart new topic

homework help

epwalz
16 Feb, 2008 - 12:47 PM
Post #1

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 7


My Contributions
was given an assignment to write a program that outputs different statements according to gender input. I have the male part working but when i input the female gender it still outputs the male response. can anyone help?

CODE

import java.util.*;
public class Greetings2
{
    //attributes
    private String fullName;
    private int age;
    
    //constructors
    public Greetings2()
    {
        char gender;
        Scanner dunno;
        dunno = new Scanner(System.in);
        System.out.println("Please type in your full name: ");
        fullName = dunno.nextLine();
        System.out.println("Please type in your gender (m/f): ");
        gender = dunno.nextLine().charAt(0);
        System.out.println("Please type in your age: ");
        age = dunno.nextInt();
        
        
        System.out.println(fullName + "(" + gender + ", " + age + ") ");
    }
    
    //methods
    public void welcome(char sex)
    {
        if (sex == 'm')
        {
           System.out.println ("Welcome, " + "Mr. " + fullName);
        }
        
        else
        {
            System.out.println("Welcome, " + "Ms. " + fullName);
        }
    }
    
    public void impression(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Macho " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Sexy " + "Ms. " + fullName);
        }
    }
    
     public void happyBirthDay(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Happy " + age +"th Birthday " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Happy " + age +"th Birthday " + "Ms. " + fullName);
        }
    }
    
  
    public void merryChristmas(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Merry Christmas, " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Merry Christmas, " + "Ms. " + fullName);
        }
    }
  
    
    
    public void runAll(char sex)
    {
          
           System.out.println("@@@ End of Demonstration @@@");
    }
}

///////////////////////////////////////////////////////////////////////////



public class Test
{
    public static void main (String[] args)
    {
       Greetings2 morris;
       morris = new Greetings2();
       morris.welcome('m');
       morris.impression('m');
       morris.happyBirthDay('m');
       morris.merryChristmas('m');
      
      
       morris.runAll('m');
    }
}

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Homework Help
16 Feb, 2008 - 12:50 PM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions


Because you call every function with the same parameter 'm', and not with the character from the input.
QUOTE
morris.welcome('m');
morris.impression('m');
morris.happyBirthDay('m');
morris.merryChristmas('m');
morris.runAll('m');


And please use code tags. code.gif
User is offlineProfile CardPM
+Quote Post

epwalz
RE: Homework Help
16 Feb, 2008 - 12:56 PM
Post #3

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 7


My Contributions
QUOTE(1lacca @ 16 Feb, 2008 - 01:50 PM) *

Because you call every function with the same parameter 'm', and not with the character from the input.
QUOTE
morris.welcome('m');
morris.impression('m');
morris.happyBirthDay('m');
morris.merryChristmas('m');
morris.runAll('m');


And please use code tags. code.gif





I'm not sure i understand what to do to fix it. What should I use for my input character. I'm using blueJ
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Homework Help
16 Feb, 2008 - 01:09 PM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
A hint:
put welcome(gender); at the end of the function public Greetings2(). After the line: System.out.println(fullName + "(" + gender + ", " + age + ") "); and it should work as you expect. However it is not a nice solution, so another hint: you should declare gender where the other attributes are, because this way it is only visible in the constructor ( Greetings2() ).
User is offlineProfile CardPM
+Quote Post

epwalz
RE: Homework Help
16 Feb, 2008 - 01:57 PM
Post #5

New D.I.C Head
*

Joined: 12 Sep, 2007
Posts: 7


My Contributions
QUOTE(1lacca @ 16 Feb, 2008 - 02:09 PM) *

A hint:
put welcome(gender); at the end of the function public Greetings2(). After the line: System.out.println(fullName + "(" + gender + ", " + age + ") "); and it should work as you expect. However it is not a nice solution, so another hint: you should declare gender where the other attributes are, because this way it is only visible in the constructor ( Greetings2() ).





I tried both of your hints but neither worked. I may be declaring gender wrong. I went into the attributes and put gender (m,f);
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Homework Help
16 Feb, 2008 - 03:40 PM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I think you've misunderstood something.

Try this (I've only fixed one function for you). I hope it is clear now.


CODE

import java.util.*;
public class Greetings2
{
    //attributes
    private String fullName;
    private int age;
    private char gender;
// so it becomes a class attribute instead of a local variable

    //constructors
    public Greetings2()
    {
        
        Scanner dunno;
        dunno = new Scanner(System.in);
        System.out.println("Please type in your full name: ");
        fullName = dunno.nextLine();
        System.out.println("Please type in your gender (m/f): ");
        gender = dunno.nextLine().charAt(0);
        System.out.println("Please type in your age: ");
        age = dunno.nextInt();
        
        
        System.out.println(fullName + "(" + gender + ", " + age + ") ");
    }
    
    //methods
    public void welcome()
    {
        if (gender == 'm') // and you access it like like this
        {
           System.out.println ("Welcome, " + "Mr. " + fullName);
        }
        
        else
        {
            System.out.println("Welcome, " + "Ms. " + fullName);
        }
    }
    
    public void impression(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Macho " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Sexy " + "Ms. " + fullName);
        }
    }
    
     public void happyBirthDay(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Happy " + age +"th Birthday " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Happy " + age +"th Birthday " + "Ms. " + fullName);
        }
    }
    
  
    public void merryChristmas(char sex)
    {
        if (sex =='m')
        {
           System.out.println ("Merry Christmas, " + "Mr. " + fullName);
        }
        else
        {
            System.out.println("Merry Christmas, " + "Ms. " + fullName);
        }
    }
  
    
    
    public void runAll(char sex)
    {
          
           System.out.println("@@@ End of Demonstration @@@");
    }
}

///////////////////////////////////////////////////////////////////////////



public class Test
{
    public static void main (String[] args)
    {
       Greetings2 morris;
       morris = new Greetings2();
       morris.welcome();
       morris.impression('m');
       morris.happyBirthDay('m');
       morris.merryChristmas('m');
      
      
       morris.runAll('m');
    }
}



User is offlineProfile CardPM
+Quote Post

bhandari
RE: Homework Help
18 Feb, 2008 - 11:41 PM
Post #7

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
And please change the topic title to something technical help you want. Though you have done some work. But homework with groundwork makes most of us to run away.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month