Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2011, 01:43 PM
Registered User
 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Ch 3 pg 88 extra challenge on appending date value!

Hello,

Has anyone tried this?

I tried it myself and due to my skill level at the moment I've only generated dates from 1st to 10th. Try out my code and teach me how I can improve my current model to make dates from 1st to 31st. :D I use NetBeans by the way.

Code:
public class ConditionalOpDate {

    public static void main(String[] args) {
        int nDate = 1;                      // Initialize Date
        nDate = (int)(1+10*Math.random());  // Generate random Date number from 1 to 10
        
        if (nDate == 1){
            System.out.println("The date is now " + nDate + "st December.");
        }   else {
            if (nDate == 2){
                System.out.println("The date is now " + nDate + "nd December.");                
            }   else {
                if (nDate == 3){
                    System.out.println("The date is now " + nDate + "rd December.");
                }   else {
                    if (nDate > 3 && nDate <= 10){
                        System.out.println("The date is now " + nDate + "th December.");
                    }
                }   
        }
    }
  }      
}

Last edited by xiao_mao_mimi; December 13th, 2011 at 01:44 PM.. Reason: added code closing
 
Old April 24th, 2012, 10:54 AM
Authorized User
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default This code seems to work...

I changed the number from 10 to 31*Math.random and added logical OR operators to the if statements. I also took out some braces and the last if statement. I ran it about 30 or more times and got a bunch of different results. All were correct.
Code:
public class ConditionalOpDate {
 public static void main(String[] args) {
  int nDate = 1;
  nDate = (int)(1+31*Math.random());
  if (nDate == 1 || nDate == 21 || nDate == 31){
            System.out.println("The date is now " + nDate + "st December.");
        }   else if (nDate == 2 || nDate == 22){
                System.out.println("The date is now " + nDate + "nd December.");         

       
            }   else if (nDate == 3 || nDate == 23){
                    System.out.println("The date is now " + nDate + "rd December.");
                }   else {
                        System.out.println("The date is now " + nDate + "th December.");
                    }
                }   
        }
 
Old May 1st, 2012, 06:15 AM
Registered User
 
Join Date: May 2012
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Here's my version of the code. It is simple and doesn't add ND,RD ot TH to the date.
Code:
import static java.lang.Math.random;
public class DateGenerate {
	public static void main(String[] args) {
		int day=(int) (1+(30*random()));
		int month=(int) (12*random());
		
		if(month>0 && month<=12) {
			if(day>0 && day<=31) {
				System.out.println("Today is " + day + "-" + month + "-2012");
			}
		}
		else {
			System.out.println("Woops! Something seems to be out of range");
		}
	}
}
 
Old May 1st, 2012, 09:17 AM
Registered User
 
Join Date: May 2012
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Here is teh one to append the end to the date using NESTED CONDITIONAL OPERATOR :)

Code:
import static java.lang.Math.random;
public class AppendDate {
	public static void main(String[] args)
	{
		int day = (int) (1+(31*random()));
		
		if(day>0 && day<=31)
		{
			System.out.println("Today is : " + day + ((day==1||day==21||day==31) ? "st" : (day==2||day==22) ? "nd" : (day==3||day==23)?"rd" : "th") + " Of December");
		}
	}
}
 
Old May 4th, 2012, 01:51 PM
Authorized User
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Nice!

I didn't know you could use the conditional operator repeatedly like that. That is wicked. Where did you see how to do that?

Last edited by skf001; May 4th, 2012 at 01:53 PM.. Reason: Forgot to include a question
 
Old May 6th, 2012, 05:21 AM
Registered User
 
Join Date: May 2012
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by skf001 View Post
I didn't know you could use the conditional operator repeatedly like that. That is wicked. Where did you see how to do that?
I learnt this when I was learning C.
I studied C from 'Let Us C' by Yeshwant Kanetkar.
Nice book :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 3, pg 88, #6 jkoyle BOOK: Beginning ASP.NET 4 : in C# and VB 4 December 5th, 2011 03:10 PM
Ch. 3 pg. 75 Try It Out Trouble rexcampbell BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 August 17th, 2010 02:29 AM
CH 5 pg 160 wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 8 November 12th, 2003 11:23 PM
CH 3 pg 76 wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 October 26th, 2003 08:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.