Please help with first homework :(

Discussion in 'Java' started by whitehatdoc, Jul 20, 2011.

  1. whitehatdoc

    whitehatdoc New Member

    Joined:
    Jul 20, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I got an assignment today and my teacher at community college isn't helpful at all, he couldn't even help us without comparing it to our code to his. My assignment is to check if a person is 21 and old enough to enter the bar. If not they cant enter, and if they are they can. However, I need to write a program that will also check what month they were born in case they are exactly 21. Finally I need to make the program loop back and allow the person to type another birthday in so they don't need to keep running the program over and over, also when they are done it should say something like press x to exit the program. Here is what I came up with so far.. any advice is helpful as this is due tomorrow! import java.util.*; //necessary for Scanner class public class BarEntry2 { public static void main(String[] args){ testTheAgeGivenTheYear() ; } public static void testTheAgeGivenTheYear() { Scanner console = new Scanner(System.in); console.nextLine(); int currentYear = 2011; int birthYear = console.nextInt() ; int age = currentYear - birthYear; int currentMonth = 7 ; boolean haveMorePatrons = true; if (age >=21) { System.out.println("You may enter the bar!") ; } else { System.out.println("You may NOT enter the bar!") ; } if (age =21) { System.out.println("Please enter the month you were born!") ; } if (currentMonth >=8) { System.out.println("You may enter the bar!") ; } System.out.println("Do you want to test anymore patrons: (Y/N) ? "); while (haveMorePatrons) { System.out.println("please enter your birth year: ") ; System.out.println("You are " + age); String ans = console.nextLine(); if (ans.equals("n") || ans.equals("N")) haveMorePatrons = false ; System.out.println ("Good Bye") ; } } }
     
  2. whitehatdoc

    whitehatdoc New Member

    Joined:
    Jul 20, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.util.*; //necessary for Scanner class
    public class BarEntry2 {
    	public static void main(String[] args){
    		testTheAgeGivenTheYear() ; 
    	}
    
    public static void testTheAgeGivenTheYear() {
    	Scanner console = new Scanner(System.in);
    	console.nextLine();
    	int currentYear = 2011;	
    	int birthYear = console.nextInt() ;
    	int age = currentYear - birthYear;
    	int currentMonth = 7 ;
    	boolean haveMorePatrons = true;
    if (age >=21) {
    	System.out.println("You may enter the bar!") ; }
    else { 
    	System.out.println("You may NOT enter the bar!") ; }
    if (age =21) {
    	System.out.println("Please enter the month you were born!") ; }
    if (currentMonth >=8) {
    	System.out.println("You may enter the bar!") ; }
    	System.out.println("Do you want to test anymore patrons: (Y/N) ? ");
    while (haveMorePatrons) {
    	System.out.println("please enter your birth year: ") ; 
    	System.out.println("You are " + age);
    String ans = console.nextLine();
    if (ans.equals("n") || ans.equals("N"))
    	haveMorePatrons = false ;
    	System.out.println ("Good Bye") ; }
    }
    }
     
    Last edited by a moderator: Jul 21, 2011
  3. whitehatdoc

    whitehatdoc New Member

    Joined:
    Jul 20, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.util.*; 
    
    public class BarEntry2 {
        public static void main(String[] args){
            testTheAgeGivenTheYear() ; 
        }
    
    public static void testTheAgeGivenTheYear() {
        System.out.println("What year where you born in?") ;
            
            if (age >=21) {
             System.out.println("Come on in :)!") ; }
            else {
             System.out.println("Sorry you're to young :(!") ; }
        Scanner console = new Scanner(System.in);
        int currentYear = 2011;    
        int currentMonth = 7 ;
        int birthMonth = console.nextInt() ;
        int birthYear = console.nextInt() ;
        int age = currentYear - birthYear;
        boolean CheckAdditionalAges = true;
        
    
        }
        
    }
    
    
    I'm so lost :(
     
    Last edited by a moderator: Jul 21, 2011
  4. whitehatdoc

    whitehatdoc New Member

    Joined:
    Jul 20, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Ok I got a lot done so i'm hoping for some help to get the final touches in.
    Code:
    import java.util.*; 
    public class BarEntryHW {
    public static void main(String[] args){
            testTheAgeGivenTheYear() ;    }
    public static void testTheAgeGivenTheYear() {
    Scanner console = new Scanner(System.in);
    int currentYear = 2011;    
    boolean checkAdditionalAges = true;
    while (checkAdditionalAges) {
            System.out.println("What year where you born in? Format:XXXX") ;
    int birthYear = console.nextInt() ;
            console.nextLine() ;
    int age = currentYear - birthYear;
             System.out.println("You are " + age ) ; 
    if (age >21) {
             System.out.println("Come on in :)") ; }
    else {
             System.out.println("You're to young") ; }
    if (age == 21) {
            System.out.println("Sorry, You might be allowed in, what month where you born? Format:X") ; }
    int birthMonth = console.nextInt() ;
            console.nextLine() ;
    if (birthMonth <=7) {
            System.out.println("You may enter") ; }
    else { 
            System.out.println("You are only 20, You may not enter :(") ; }
            System.out.println("Do you want to test any additional ages: (Y/N) ? ") ;
            String ans = console.nextLine() ;
    if (ans.equals("n") || ans.equals("N")) {
            checkAdditionalAges = false;
            System.exit(0); }
    else {
         (ans.equals("y") || ans.equals("Y")) 
             checkAdditionalAges = true ; }
        }
    }
     
    Last edited by a moderator: Jul 21, 2011
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    See the reason for why your posts are being edited and that link will help you format your posts better
     
  6. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    when you get your first homework, make sure you try it your own because you wont be learning anythnig unless :S
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice