Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 13th, 2011, 01:43 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old April 24th, 2012, 11:54 AM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.");
                    }
                }   
        }
Reply With Quote
  #3 (permalink)  
Old May 1st, 2012, 07:15 AM
Registered User
Points: 31, Level: 1
Points: 31, Level: 1 Points: 31, Level: 1 Points: 31, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Location: Mumbai,India
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");
		}
	}
}
Reply With Quote
  #4 (permalink)  
Old May 1st, 2012, 10:17 AM
Registered User
Points: 31, Level: 1
Points: 31, Level: 1 Points: 31, Level: 1 Points: 31, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Location: Mumbai,India
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");
		}
	}
}
Reply With Quote
  #5 (permalink)  
Old May 4th, 2012, 02:51 PM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 02:53 PM. Reason: Forgot to include a question
Reply With Quote
  #6 (permalink)  
Old May 6th, 2012, 06:21 AM
Registered User
Points: 31, Level: 1
Points: 31, Level: 1 Points: 31, Level: 1 Points: 31, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Location: Mumbai,India
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 :)
Reply With Quote
Reply


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03: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



All times are GMT -4. The time now is 07:27 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.