NOTICE: These solutions are here so that you are able to check your work or if you are completely baffled by a problem and have given up all hope that you can solve the problem. Remember, your classroom work is not graded. The grade you receive in Programming 1 is entirely based on your quiz, test and project scores.
Problem 1 |
public void hi()
{
char firstChar,secondChar,thirdChar;
firstChar='H';
secondChar='I';
thirdChar='!';
System.out.println("The first character is " + firstChar);
System.out.println("The second character is " + secondChar);
System.out.println("The third character is " + thirdChar);
System.out.println("Together they spell " + firstChar + secondChar + thirdChar);
} |
Problem 2 |
public void math()
{
double num1, num2,sum,product;
System.out.print("Enter a value for X: ");
num1=in.nextDouble();
System.out.print("Enter a value for Y: ");
num2=in.nextDouble();
product=num1*num2;
sum=num1+num2;
System.out.println(num1+"*"+num2+" = "+product);
System.out.print(num1+"+"+num2+" = "+sum);
}
|
Problem 3 |
public void name_and_pet()
{
String name, name2, animal;
System.out.print("Enter your first name: ");
name=in.next();
System.out.print("Enter your second name: ");
name2=in.next();
System.out.print("Enter your favorite animal: ");
animal=in.next();
System.out.println();
System.out.print("Hi "+name+". My last name is also "+name2+"! I think the "+animal+" is a great animal.");
} |
Problem 4 |
public void cost_of_loaves()
{
Double num1, num2, answer, answer2;
System.out.print("Enter a price (in pennies) for the loaf of bread: ");
num1=in.nextDouble();
System.out.print("Enter the number of loaves to buy: ");
num2=in.nextDouble();
System.out.println();
answer=num1*num2/100;
answer=Math.round(answer*100.0)/100.0;
System.out.print("The total comes to: $"+answer);
} |
Problem 5 |
public void pounds_per_inch()
{
double num1, num2, answer;
System.out.print("Enter your weight in pounds: ");
num1=in.nextDouble();
System.out.print("Enter your height in inches: ");
num2=in.nextDouble();
System.out.println();
answer=num1/num2;
answer=Math.round(answer*100.0)/100.0;
System.out.print("You weigh "+answer+" pounds per inch!");
} |
Problem 6 |
public void calories()
{
double pieces, miles;
System.out.print("Enter the number of pieces of pizza eaten: ");
pieces=in.nextDouble();
miles=pieces*3.75;
miles=Math.round(miles*10.0)/10.0;
System.out.print("You will have to run "+miles+" to burn that off.");
} |
Problem 7 |
public void room_volume()
{
double height, width, length, volume;
System.out.print("Enter the room's height (in feet): ");
height=in.nextDouble();
System.out.print("Enter the room's width (in feet): ");
width=in.nextDouble();
System.out.print("Enter the room's length (in feet): ");
length=in.nextDouble();
System.out.println();
volume=height*width*length;
System.out.print("The volume of the room is "+volume+" cubic feet.");
} |
Problem 8 |
public void triangle_area()
{
double base, height, area;
System.out.print("Enter the base (inches): ");
base=in.nextDouble();
System.out.print("Enter the height (inches): ");
height=in.nextDouble();
System.out.println();
area=.5*base*height;
System.out.print("The triangle's area is "+area+" square inches.");
} |
Problem 9 |
public void hours_slept()
{
double day, month, year, todayDay, todayMonth, todayYear, totalDays, totalHours;
System.out.print("Enter the day you were born: ");
day=in.nextDouble();
System.out.print("Enter the month you were born: ");
month=in.nextDouble();
System.out.print("Enter the year you were born: ");
year=in.nextDouble();
System.out.println();
System.out.print("Enter today's day: ");
todayDay=in.nextDouble();
System.out.print("Enter today's month: ");
todayMonth=in.nextDouble();
System.out.print("Enter today's year: ");
todayYear=in.nextDouble();
totalDays=((todayYear-year)*365)+((todayMonth-month)*30)+(todayDay-day);
totalHours=totalDays*8;
System.out.print("You have slept "+totalDays+" days for a total of "+totalHours+" hours.");
}
} |
Problem 10 |
public void share_value()
{
double shares, value, num, den, total;
System.out.print("Enter the number of shares: ");
shares=in.nextDouble();
System.out.print("Enter the whole portion of the share's value: ");
value=in.nextDouble();
System.out.print("Enter the numerator: ");
num=in.nextDouble();
System.out.print("Enter the denominator: ");
den=in.nextDouble();
System.out.println();
total=(shares*value)+(shares*num/den);
System.out.print("The value of your stock is $"+total);
} |
Problem 11 |
public void calc_bill()
{
double hours, bill, sur, tax, total;
System.out.print("Enter the number of kilowatt hours: ");
hours=in.nextDouble();
bill=hours*3.25/100;
bill=Math.round(bill*100.0)/100.0;
System.out.println("Basic Electric bill: $"+bill);
sur=bill/10;
sur=Math.round(sur*100.0)/100.0;
System.out.println("10% Surcharge: $"+sur);
tax=bill*.03;
tax=Math.round(tax*100.0)/100.0;
System.out.println("3% Utility Tax: $"+tax);
total=sur+tax+bill;
total=Math.round(total*100.0)/100.0;
System.out.print("Total due: $"+total);
} |
Problem 12 |
public void dividing()
{
int num1, num2, answer, remainder;
System.out.print("Enter a number: ");
num1=in.nextInt();
System.out.print("Enter another number (smaller than the first number): ");
num2=in.nextInt();
answer=num1/num2;
remainder=num1%num2;
System.out.println();
System.out.print(num1+" divided by "+5+" is "+4+" with a remainder of "+remainder);
} |
Problem 13 |
public void calc_boxes()
{
int num1, large, small;
System.out.print("Enter the number of flower pots ordered: ");
num1=in.nextInt();
large=num1/4;
small=num1%4;
System.out.print("You will need "+large+" large boxes and "+small+" small boxes.");
} |
Problem 14 |
public void calc_boxes2()
{
int num1, large, medium, small;
System.out.print("Enter the number of flower pots ordered: ");
num1=in.nextInt();
large=num1/8;
medium=num1%8/4;
small=num1%8%4;
System.out.print("You will need "+large+" large boxes, "+medium+" medium boxes, and "+small+" small boxes.");
} |
Problem 15 |
public void generate_num()
{
int rndmNum;
rndmNum=(int)(Math.random() *15) +1;
System.out.println("The number selected from 1 to 15 is: "+rndmNum);
rndmNum=(int)(Math.random() *6) +15;
System.out.println("The number selected from 15 to 20 is: "+rndmNum);
rndmNum=(int)(Math.random() *2) +75;
System.out.print("The number selected from 75 to 76 is: "+rndmNum);
} |
Problem 16 |
public void doing_math()
{
int ran1, ran2, add, sub, mult, div, remain;
ran1=(int)(Math.random()*20)+1;
ran2=(int)(Math.random()*20)+1;
add=ran1+ran2;
sub=ran1-ran2;
mult=ran1*ran2;
div=ran1/ran2;
remain=ran1%ran2;
System.out.println("The numbers selected are: "+ran1+" and "+ran2);
System.out.println(ran1+" + "+ran2+" = "+add);
System.out.println(ran1+" - "+ran2+" = "+sub);
System.out.println(ran1+" * "+ran2+" = "+mult);
System.out.println(ran1+" / "+ran2+" = "+div+" with a remainder of "+remain);
} |
|