There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen blue screen of death boot computer connection crash css dell display driver drivers email error excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking outlook outlook 2003 outlook express partition password printer problem router slow software sound sprtcmd.exe startup trojan usb virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Java help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
SpyKiller112's Avatar
Senior Member with 510 posts.
 
Join Date: Aug 2004
Experience: Intermediate
15-Oct-2007, 12:15 PM #1
Java help
Alrighty, so I'm working on a small assignment and I'm a bit stuck.

The assignment:
Code:
Write a program that computes the day of a week given Julian day.

People who deal with historical dates use a number called the Julian day to calculate the number of days between two events. The Julian day is the number of days that have elapsed since January 1, 4713 B.C. For example, the Julian day for October 16, 1956, is 2435763.
There are formulas for computing the Julian day from a given date, and vice versa. One very simple formula computes the day of the week from a given Julian day:

Day of the week = (Julian day + 1) % 7

Where % is the Java modulus operator. This formula gives a result of 0 for Sunday, 1 for Monday, and so on, up to 6 for Saturday. For Julian day 2435763, the result is 2 (Tuesday). 

You have to write a java application (program) that requests and inputs a Julian day, computes the day of the week using the formula, and then displays the name of the day that corresponds to that number. Your output might look like this:

Enter a Julian day number:
2451545
Julian day number 2451545 is Saturday.
What I have so far:

Code:
import java.util.Scanner;

public class Julian
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		int Julian;

		System.out.print("Enter Julian day: ");
		Julian = keyboard.nextInt();

		double dayofweek = (String) ((Julian + 1) % 7);

		dayofweek = dayofweek.replace("0", "Sunday");
		dayofweek = dayofweek.replace("1", "Monday");
		dayofweek = dayofweek.replace("2", "Tuesday");
		dayofweek = dayofweek.replace("3", "Wednesday");
		dayofweek = dayofweek.replace("4", "Thursday");
		dayofweek = dayofweek.replace("5", "Friday");
		dayofweek = dayofweek.replace("6", "Saturday");

		System.out.println("The Julian day number " + Julian + "is " + dayofweek);

	}
}
I'm trying to make my program change the value returned after the equation "(Julian + 1) % 7)" to a day of the week. The only thing is, I've only used the replace method with strings. any hints?
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,716 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
15-Oct-2007, 07:09 PM #2
Shouldn't this thread be in the Development forum?

Why are you trying to perform string operations on a numerical object?

What about something like this (pseudo-code):

dayofweek = (Julian + 1) % 7;
switch (dayofweek) {
// case day, set var to String representation of day
}

System.out.println("The Julian day number " + Julian + "is " + [week-day string, whatever that is]);

I think a switch statement would be of great help here.

Peace....
SpyKiller112's Avatar
Senior Member with 510 posts.
 
Join Date: Aug 2004
Experience: Intermediate
15-Oct-2007, 08:02 PM #3
Hey, thanks anyway. I learned how to use if and if else and that worked out well.
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,716 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
15-Oct-2007, 08:18 PM #4
Cool! if/then/else will work as well (as you have learned) but for constructs like yours, a switch statement might be a more streamlined approach.

Peace...
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:28 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.