Monday, January 23, 2017

Programming By Doing Lesson 9

Programming By Doing Lesson 9 - My code

//declare the class public - available to anyone
public class VariablesAndNames
{
//declare the main method which does not return anything
public static void main ( String[] args )
{
//declare five number variables
int cars, drivers, passengers, cars_not_driven, cars_driven;
double space_in_a_car, carpool_capacity, average_passengers_per_car;

//assign the variables a value
cars = 100;
space_in_a_car = 4.0;
drivers = 30;
passengers = 90;
cars_not_driven = cars - drivers;
cars_driven = drivers;
carpool_capacity = cars_driven * space_in_a_car;
average_passengers_per_car = passengers / cars_driven;

//print each item on a separate line
System.out.println( "There are " + cars + " cars available." );
System.out.println( "There are only " + drivers + " drivers available." );
System.out.println( "There will be " + cars_not_driven + " empty cars today." );
System.out.println( "We can transport " + carpool_capacity + " people today." );
System.out.println( "We have " + passengers + " to carpool today." );
System.out.println( "We need to put about " + average_passengers_per_car + " in each car." );
}
}

And my output in command prompt:
c:\Users\jenni\OneDrive\Documents\CompSci>javac VariablesAndNames.java

c:\Users\jenni\OneDrive\Documents\CompSci>java VariablesAndNames
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.

No comments:

Post a Comment