Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-12-2008, 03:46 AM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
annoyingzhang is on a distinguished road
Default CompSci 1 Novice, need some help
So i have a program due on Mon. My teacher REQUIRES a flowchart, so i spent an hour doing the flowchart, all check and done, after that i start to program and now i have the program running but having the wrong output. Can anyone help me?

This is the lab description:

Create a new file named Birthday.java.

Write a program to determine the day of the week that a person was born given his or her birth date. Following are the steps you should use to find the day of the week corresponding to any date in the 20th century.

a. Divide the last two digits of the birth year by 4. Put the quotient (ignoring the remainder) in total. (For example, if the person was born in 1983, divide 83 by 4 and store 20 in total.)

b. Add the last two digits of the birth year to total.

c. Add the last two digits of the birth date to total.

d. Use the following table, find the “month number” and add it to total.

Jan = 1 Feb = 4 Mar = 4 Apr = 0 May = 2 Jun = 5
July = 0 Aug = 3 Sept = 6 Oct = 1 Nov = 4 Dec = 6

e. If the year is a leap year and if the month you are working with is either Jan or Feb, then subtract 1 from total.

f. Find the remainder when total is divided by 7. Look up the remainder in the following table to determine the day of the week the person was born. Note that you cannot use this procedure if a person was born before 1900;

1 = Sunday 2 = Monday 3 = Tuesday 4 = Wednesday
5 = Thursday 6 = Friday 0 = Saturday

The user should be prompted to enter the birth month by number( 1 for Jan, 2 for Feb...) then the birth date and last the year in the format 1983. Make sure to give an error message if the birth year is before 1900.






And here's the code that i've written so far. Also i have no clue on why certain varibles cannot be used out side of the "{}" in the if else statements, also i have no clue on how to do dates for year 2000 and above, i used my birth date 11/05/1992 as a reference and is ONE day off. I'm totally bummed cause i spend 3 hours trying to fix all these problems. Help would be very nice and turns out my friends suck even more than i do, they can't even get a output!! And admin sry if this in the wrong place!! this is first post ever. Thanks for helping.



//Roger Zhang
//AP CompSci 2
//Freed

import java.util.Scanner;

public class Birthday
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please input birthdate in mm/dd/yyyy");
int month = in.nextInt();
int date = in.nextInt();
int year = in.nextInt();

//End of Block A, user inputs

if (year > 2100)
{
System.out.println("Please input a year in the 20th century!");
}
else
{
if (year < 1900)
{
System.out.println("Please input a year in the 20th century!");
}
else
{
if (year >= 2000)
{
int yearOne = year - 2000;
//System.out.println(yearOne);

}
else
{
if (year >= 1900)
{
int yearOne = year - 1900;
//System.out.println(yearOne);
//End of Block B, to determine the last two digits of the year
int total = (yearOne/4)+yearOne+date;
//System.out.println(total);
if (month == 01||month == 10)
{
int total2 = total + 1;
}
else
{
if (month == 05)
{
int total2 = total + 2;
}
else
{
if (month == 8)
{
int total2 = 3;
}
else
{
if (month == 02||month==03||month==11)
{
int total2 = total + 4;
//System.out.println(total2);
}
else
{
if (month == 9||month==12)
{
int total2 = total + 6;
}
else
{
if (month == 06)
{
int total2 = total + 5;
}
else
{
if (month == 07||month == 04)
{
int total2 = total + 0;
//End of Block C, to add a corresponding number to the month
}
}
}
}
}
}
}
}
}
}
}
int leapYear = year%4;
//System.out.println(leapYear);
if (leapYear == 0)
{
if (month ==01||month==02)
{
int month2 = month -1;
//System.out.println (month2);
}
}
int yearTwo = total2%7;
if (yearTwo == 1)
{
System.out.println("This person was born on a Sunday");
}
else
{
if (yearTwo == 2)
{
System.out.println("This person was born on a Monday");
}
else
{
if (yearTwo==3)
{
System.out.println("This person was born on a Tuesday");
}
else
{
if (yearTwo == 4)
{
System.out.println("This person was born on a Wednesday");
}
else
{
if (yearTwo == 5)
{
System.out.println("This person was born on a Thursday");
}
else
{
if (yearTwo==6)
{
System.out.println("This person was born on a Friday");
}
else
{
if (yearTwo==0)
{
System.out.println("This person was born on a Saturday");
}
}
}
}
}
}

}

}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-12-2008, 04:32 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Some suggestions...
Some suggestions about your code:
  • Try not to repeat code segments that are the same (example: same output):
Code:
if (year > 2100)
{
System.out.println("Please input a year in the 20th century!");
}
else
{
if (year < 1900)
{
System.out.println("Please input a year in the 20th century!");
}
... looks much better (and is easier to read) as:

Code:
if (year > 2100 || year < 1900)
{
System.out.println("Please input a year in the 20th century!");
}
  • When posting code, use code tags (third icon above right to left)... see code examples above
  • Breakout your code into methods depending on the functionally (right now it's spagetti code... hard to read). Don't know if you've seen methods yet.
  • Give your variable names some meaning: don't use "year", better to use "inputYear" or something that has meaning.
  • To be able to debug your code and follow the flow, use "System.out.println" through out you program with useful info so you know what's happening in your program... example:
Code:
System.out.println ("Total year amount after first calc =" + totalYear);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-12-2008, 04:42 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Code:
 if (yearTwo == 1)
{
System.out.println("This person was born on a Sunday");
}
else
{
if (yearTwo == 2)
{
System.out.println("This person was born on a Monday");
}
else
If the conditions tested for are mutually exclusive (ie only one is true) you can change the if {} else to:

Code:
if (yearTwo == 1){
  System.out.println("This person was born on a Sunday");
}else if(yearTwo == 2){
  System.out.println("This person was born on a Monday");
}else if(...
I find this sequence easier to read and follow.
Also in your case, you could use the switch statement. It's made for selecting a statement based on the value of an int.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-12-2008, 04:49 AM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
annoyingzhang is on a distinguished road
Default
Thank your for the suggestions. Main problem i still have is when i want to reuse a certain variable later on in the program and it so happens to be in a if else statement it cannot be used. The one and only error i come up with is that the program can't find that certain variable. Any ideas?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-12-2008, 04:58 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Define it where it will be in scope for all the places you want to use it.

Not sure why you want to reuse it. If you don't need its current value, then leave it defined locally and define a new one when/where you need it. Variables with TOO much scope can be dangerous.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-12-2008, 05:23 AM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
annoyingzhang is on a distinguished road
Default
Okay, got no clue what you just said!! lol, talking to a newb here. I will try again in a exampl wayy. Okay so here's a if else statement that determines whether the year the user inputs is in the 20th century (1900 to over 2000) I need the last two digits so i need to subtract either 2000 or 1900 to get it. So if it's >= 2000 you subtract it by 2000 and so forth with the 1900's. What i want to do is determine the last 2 digits and still use it later on in the code. I have this so far:

PHP Code:
        if (year 2100|| year 1900)
        {
            
System.out.println("Please input a year in the 20th century!");
        }
        else
        {
            if (
year >=2000)
            {
                
int yearOne year-2000
                
System.out.println(yearOne);
            }
            else 
            {
                
int yearOne year-1900;
                
System.out.println(yearOne); 
            }
        } 
So whats next? I found out i can do one thing, write all the math operations for the 1900's and then make ANOTHER set for the 2000's, wouldn't it be more easy if you can just determine beforehand if the user's number is in the 2000's or 1900's and then do the math operations necessary instead of having two branches? The variable "yearOne" will be quite handy for me later on.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-12-2008, 03:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
in the 20th century (1900 to over 2000)
Check your definition here.
1900 to 1999 (or 1901 to 2000) would be in the 20th century. Over 2000 is in the 21st century.

You need to read in your text about the scope of variables. the scope of a variable is usually within the enclosing {}s. To have the value of yearOne available outside the if else statements you need to define the variable outside of those statements. Either at the method level or at the class level.

Last edited by Norm; 10-12-2008 at 03:31 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-12-2008, 04:16 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default scope
Originally Posted by annoyingzhang View Post
Okay, got no clue what you just said!!
Code:
public class Zhang
{
    String year;//
    Zhang(String someYear)
    {
        year = someYear;
    }
    public static void main(String[] args)
    {
        Zhang anotherAnnoyingPhipper = new Zhang("2010");//
        // Determine the last 2 digits and still use it later on in the code.
        int lastChars = anotherAnnoyingPhipper.year.length();
        // decrement should be moved to named variable for beginner:
        char lastDigit = anotherAnnoyingPhipper.year.charAt(--lastChars);
        char nextToLastDigit = anotherAnnoyingPhipper.year.charAt(--lastChars);
        // This is not working too good but look and see where I placed String year;
        // Note that it is outside of the curly braces ...... now year is available 
        // beyond the scope of this method....
    }
}
__________________
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-12-2008, 07:21 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
annoyingzhang is on a distinguished road
Default
okay guys thanks for all your help, but i got it fixed. HAHHA it was really stupid. I should've declare my variable before the if else statements so i can use it later.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-12-2008, 10:02 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Exactly....
That is what is called Scope. I looked for the simpleist definition of "scope of a variable" and found the following (look at the "Java Variables and Scope" section at the end):
Object Oriented Scope
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello to all, a java novice here, please help theneed4doyen Introductions 5 07-22-2008 06:52 PM


All times are GMT +2. The time now is 03:25 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org