7 Replies - 599 Views - Last Post: 12 September 2012 - 01:07 PM Rate Topic: ***** 1 Votes

#1 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

final CONSTANTS and if-then Boolean statements.

Posted 11 September 2012 - 04:16 PM

Hello all,

I would like to start by describing the goal my current project and the issue(s) I have with my current design/code.

The goal is to create a prompt that calculates speeding violation costs and output the fines and misc. information of the

driver.

Now what's troubling me is creating IF...THEN statements with the Boolean operator.

E.g. The prompt would print "Were you driving in a school zone? (True/False)"

I want to use the scanned input of True or False and make it do something else such as adding to the original fine.

Here's what I have so far, it's quite a lengthy chunk of code, but I appreciate any guidance that I receive.

I'm not here to ask for a solution then run with it, I'm here to seek a viable fix that I can learn from. Thank you.

P.S. I've commented out a large portion of my code because I'm still designing the logic behind my program.

import java.io.*;
import java.util.*;

public class Program1
{

	public static void main (String args[] ) throws Exception
	{
	
	//Insert CONSTANTS here 
	//final int SPEED_LIMIT = 
	//final int BASE_FINE = 100
	//final int UNDER_TWENTY = 
    //final int OVER_TWENTY = 
	//final int CONSTR_FINE = *2
	//final int UNDER_FINE = 300
	

	Scanner kbd = new Scanner (System.in);
	
	//1st printed line displaying Penn State Police Department
	System.out.println ("As a courtesy of the Pennsylvania State Police Department," + 
	"\n" + "you have received a written warning for violating Pennsylvania motor" + 
	"\n" + "vehicle law(s):" + "\n"	);

	//FirstName prompt; asking user to input legal first name
	String FirstName;
	System.out.print( "Enter your legal first name: ");
	FirstName = kbd.nextLine();
	System.out.println ( "\n" + "You have entered " + FirstName + " as your legal first name. " + "\n");
	System.out.println ( "____________________________________________________________________" + "\n");
	
	//LastName prompt; asking user to input legal last name
	String LastName;
	System.out.print( "Enter your legal last name: ");
	LastName = kbd.nextLine();
	System.out.println("\n" + "You have entered " + LastName + " as your legal last name. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	
	//DriverAge prompt; asking user for his/her age in "x" years
	System.out.print( "Enter your age (in years): ");
	int DriverAge;
	DriverAge = kbd.nextInt();
	System.out.println("\n" + "You have entered " + DriverAge + " as your age in years. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	
	//SpeedLimit prompt; asks for enforced speed limit 
	System.out.print( "Enter the enforced speed limit (in Miles Per Hour): ");
	int SpeedLimit;
	SpeedLimit = kbd.nextInt();
	System.out.println("\n" + "You have entered " + SpeedLimit + " MPH" + " as the enforced speed limit. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	
	//DriverSpeed prompt; asks for how fast the driver was going
	//Used to compare to SpeedLimit
	System.out.print( "Enter your actual speed (in Miles Per Hour): ");
	int DriverSpeed;
	DriverSpeed = kbd.nextInt();
	System.out.println("\n" + "You have entered " + DriverSpeed + " MPH" + " as your actual speed of travel. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	
	//Boolean operator; asks whether or not violation occured in construction zone
	System.out.print( "Speeding in construction zone? (True/False) ");
	boolean ConZone;
	ConZone = kbd.nextBoolean();
	
	//IF...ELSE for Boolean True or False
	//Not functional, not printing the println; needs work.
	if boolean ConZone == True {
	System.out.println("\n" + "You were speeding in a construction zone. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	}
	else {
	System.out.println("\n" + "You were not speeding in a construction zone. " + "\n");
	System.out.println("_______________________________________________________________" + "\n");
	}
	

	
	//IF ... THEN 
	//5 MPH over speed limit 
	//Report and all prompts will still appear.
	
	/*if ( milesOverLimit > 5)
	{
		System.out.println("Speeding over limit by at least 5 MPH." );
	}
	else
	{
		System.out.println("No speeding violation, have a nice day and drive safe." );
	}
	*/
	
	//Else if driver is over limit by 20 MPH, $30/per 5 MPH over limit.
	
	
	
	
	//Else if driver is more than 20 MPH over limit, $30/per 5 MPH over limit.
	
	
	
	
	//If violation in construction zone, DOUBlE the fine.
	
	
	
	//Underage penalty 
	//*2 original fine if speeder < 21 years of age and more than 20 MPH over limit
	
	/*
	if ( age < 21 && milesOverLimit > 20 )
	{ 
		System.out.println("Underage driving speeders receive a $300 fine." );
		fine += 300;
	}
	else
	{
		System.out.println("You are of age to drive, no additional $300 fine added." );
	}
	
	if ( CONSTRU = True );
	{
		System.out.println("Traffic violation occured in construction zone." );
		System.out.println("Original fine will be doubled for speeding in a construction zone." );
		
	}
	*/
	
	//Decorative Border
	System.out.println("**************Pennsylvania State Police Department*************" + "\n");
	System.out.println("***************************************************************" + "\n");
	
	/* Have to declare CONSTANTS and variables soon...
	/* IF ... Else statements
	/* IF ... Then statements
	/* Do NOT mix the rates, either charged $30/5 MPH over OR $50/5 MPH over.
	/* IF CONSTRUCTION ZONE, THEN numerically *2 the fine
	/* IF driver < 21 AND && > 20 MPH over limit THEN + $300 as underage speeder fine
	/* This is add-on fine, no doubling for the underage, only for construction does it double
	*/
	//Summary Report output below:
	System.out.println(" Last Name: " + LastName + "\n");
	System.out.println(" First Name: " + FirstName + "\n");
	System.out.println(" Age of driver: " + DriverAge + "\n");
	//System.out.println(" Enforced Speed Limit: " + "$" + SpeedLimit + "\n");
	//System.out.println(" Miles Per Hour Over Speed Limit: " + "$" + SpeedOver + "\n");
	//System.out.println(" Base fine for violation: " + "$" + BaseFine + "\n");
	//System.out.println(" Construction Zone Penalty Fine: " + "$" + CONSTR_FINE + "\n");
	//System.out.println(" Driving Underage Violation: " + "$" + UNDERAGE_FINE + "\n");
	//System.out.println(" Accumulated Total of Violation: " + "$" + Total + "\n");
		
	//Decorative Border
	System.out.println("***************************************************************" + "\n");
	System.out.println("**********Drive safe and we hope you have a nice day!**********" + "\n");

	
	} // END MAIN
} // END class Program1
		




Error Prompt in cmd:
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: final CONSTANTS and if-then Boolean statements.

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: final CONSTANTS and if-then Boolean statements.

Posted 11 September 2012 - 04:23 PM

Quote

 if boolean ConZone == True {


should be

 if (ConZone == true) {

Was This Post Helpful? 1
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,488
  • Joined: 05-May 05

Re: final CONSTANTS and if-then Boolean statements.

Posted 11 September 2012 - 04:25 PM

The if statement takes a boolean expression, and a boolean variable is a boolean expression, so you have:

boolean myBoolean = true;
if(myBoolean) {
     //do something
}



I don't see why you decided to include the type in that first if statement, but not the others. The type name (i.e. boolean) is only written for a variable declaration or method parameter.

This post has been edited by blackcompe: 11 September 2012 - 04:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: final CONSTANTS and if-then Boolean statements.

Posted 11 September 2012 - 04:32 PM

View Postg00se, on 11 September 2012 - 04:23 PM, said:

Quote

 if boolean ConZone == True {


should be

 if (ConZone == true) {


Oh, whoops. That was a careless mistake, thanks for the catch, g00se.

View Postblackcompe, on 11 September 2012 - 04:25 PM, said:

The if statement takes a boolean expression, and a boolean variable is a boolean expression, so you have:

boolean myBoolean = true;
if(myBoolean) {
     //do something
}



I don't see why you decided to include the type in that first if statement, but not the others. The type name (i.e. boolean) is only written for a variable declaration or method parameter.


If I understand your question, what I wanted was Boolean for the construction zone (True/False).

The other If...Then statements are involved in calculating the accumulated fine and costs of the ticket.

I tried to use If...Then...Else statements for that.

This is where I have to go back to the drawing board since I'm not sure how to input the calculations.

The print lines, strings, and Booleans I can handle okay, but I have some work to do for the ticket costs and

the various conditions (> 20 MPH, $30/5 MPH over, etc) of the ticket.

Thanks for the guidance guys, will update sooner or later.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: final CONSTANTS and if-then Boolean statements.

Posted 11 September 2012 - 04:39 PM

Quote

if ( CONSTRU = True );



Be careful with that too. Not only is there a stray semicolon, but that's not a comparison, but an assignment
Was This Post Helpful? 1
  • +
  • -

#6 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: final CONSTANTS and if-then Boolean statements.

Posted 12 September 2012 - 12:53 PM

Could someone possibly explain how I can implement more constants using
[/code] final int CONSTANT [/code]?

I'm having trouble designing the program where it can use CONSTANTS effectively to calculate speeding costs.
Was This Post Helpful? 0
  • +
  • -

#7 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: final CONSTANTS and if-then Boolean statements.

Posted 12 September 2012 - 12:58 PM

Sorry for the double post, not sure if new members like me have the ability to edit posts, but I have fixed the original

post above.

Could someone possibly explain how I can implement more constants using constants?

E.g.
 final int CONSTANT 


I'm having trouble designing the program where it can use CONSTANTS effectively to calculate speeding costs.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: final CONSTANTS and if-then Boolean statements.

Posted 12 September 2012 - 01:07 PM

the final keyworks make a constant.
So

static final int NB_CARDS = 52;

defines the constant NB_CARDS whose value is 52. Value that can't be changed.
Principal use a constant is that if it is used at more than one place in the code and it need to be changed it will have to be changed only at one place.


Most of the time, constants are also static, as they can't be changed there is no reason that their value cannot be shared by all instance of a class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1