Chapter 2 Programming Exercises ( Even Number)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

2.10 Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water. The formula to compute the energy is Q = M * (finalTemperature - initialTemperature) * 4184 where M is the weight of water in kilograms, temperatures are in degrees Celsius, and energy Q is measured in joules.

import java.util.Scanner; public class CalculateEnergy { public static void main(String[] args) { int M, currentTemperature, initialTemperature; Scanner input = new Scanner (System.in); System.out.print(" Please Enter in weight of water in kilograms, initial temperature and final temperature: "); M = input.nextInt(); initialTemperature = input.nextInt(); currentTemperature = input.nextInt(); int Q = M * (currentTemperature - initialTemperature) * 4184; System.out.println(" The energy for water that weighs" + M + " kilograms whose starting temperature was " + initialTemperature + " and final temperature is " + currentTemperature + " is"+ Q + " Joules "); input.close(); } }

2.2 Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas: area = radius * radius * p volume = area * length

import java.util.Scanner; public class ComputeVolumeArea { public static void main(String[] args) { double length, volume, area, radius; final double PI = 3.14159; Scanner input = new Scanner (System.in); System.out.print(" Please Enter in the radius and length of a cylinder"); radius = input.nextDouble(); length = input.nextDouble(); // calculate area area = Math.pow(radius,2) * PI; // calculate volume volume = area * length; System.out.println("The area of a cylinder whose length is " + length + " and " + " radius is " + radius + " is " + area); System.out.println("The volume of a cylinder whose length is " + length + " and " + " radius is " + radius + " is " + volume); input.close(); } }

2.4 Write a program that converts pounds into kilograms. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms.

import java.util.Scanner; public class ConvertLbsToKilograms { public static void main(String[] args) { double lbs, kilograms; final double KILO = 0.454; Scanner input = new Scanner (System.in); System.out.print(" Please enter in number of pounds"); lbs = input.nextDouble(); // calculate conversion kilograms = lbs * KILO; System.out.println(lbs + "pounds is " + kilograms + " Kilograms. "); input.close(); } }

2.6 Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.

import java.util.Scanner; public class SumOfIntegers{ public static void main(String args[]) { Scanner input = new Scanner (System.in); System.out.print(" Please enter in a digit between 0 and 1000: "); int number = input.nextInt(); int sum = 0; int num = number; while (num > 0) { int lastDigit = num % 10; sum += lastDigit; num /= 10; } System.out.println("Sum of digits : "+sum); input.close(); } }


Ensembles d'études connexes

chapter 24 dynamic study modules

View Set

Ch. 23 Asepsis and Infection Control Quiz

View Set

Major Quiz 2 (Culture, Social Structure and Socialization)

View Set

5.1 Epithelial Tissue: Surfaces, Linings, and Secretory Functions

View Set

World History- The French Revolution and Napoleon

View Set