1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình JAVA Bài tập 2

7 431 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 27,42 KB

Nội dung

Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA Lập trình JAVA

Exercises of Week EXERCISES OF WEEK Exercise 1: The following program uses primitive data type short: public class ShortType{ public static void main ( String [] args ) { short value = 32; System.out.println("A short: " + value); } } value in this program is a variable—a name for a section of memory that holds data using a particular data type In this case value is the name for 16 bits of main memory that uses short to represent an integer (The next chapter says much more about variables.) This program puts the value 32 into value Then it writes out: A short: 32 In other words, that line of the program examines the variable and writes out what it finds Your Job: Create a file called ShortType.java that contains this program (Copy and paste will greatly speed this up.) Compile and run the program Check what it writes onto the screen Now edit the program so that the 32 is changed to some other small number, say 356 Compile and run the program Everything should be fine Next change the number to 35000 and try to compile and run the program This number is too large to work with the data type short (in other words, it cannot be represented in 16 bits using data type short.) What happens? Now edit the program (don't change the 35000) so that the data type is int Compile and run the program Is there a difference? Exercise 2: The following program also primitive data type double This program computes and writes out the value of exp( 32 ) This is the number "e" raised to the Exercises of Week power 32 ("e" is the base of natural logarithms Don't worry much about this The point of the program is not the math but the floating point numbers.) public class ExponentialExplosion{ public static void main ( String [] args){ double value = 32; System.out.println("e to the power value: " + Math.exp( value ) ); } } Compile and run the program Does it compile and run correctly? Now change the 32 to larger and larger numbers until something goes wrong Exercise 3: The following program uses primitive data type char: public class CharAssassination{ public static void main ( String[] args){ char ch = 'A' ; System.out.println("A char: " + ch ); } } The variable ch is 16 bits of main memory that uses the char data type to represent characters The a bit pattern that represents 'A' is placed in it The program writes: A char: A Do the following: • • • • • Change the 'A' into 'Z' and compile and run Change the 'A' into 'AA' and try to compile the program Change the 'A' into ' ' and compile and run the program o Notice carefully: there is a single space between the two ' marks Change the 'A' into '' and try to compile o Notice carefully: there is no character between the two ' marks Change the 'A' into "A" and try to compile the program o (The double quotes " signify a String, which is something different from a char) Observe and explain what works and what does not work in the above Exercises of Week Exercise 4: Write a Java program to declare two integer variables, one float variable, and one string variable and assign 10, 12.5, and "Java programming" to them respectively Then display their values on the screen public class DeclareVariable{ public static void main (String [] arg){ / / {Write your code here //} } } Exercise 5: Write Java program to allow the user to input two integer values and then the program prints the results of adding, subtracting, multiplying, and dividing among the two values Ex: Enter value a: 30 Enter value b: 10 The result of adding is 40 The result of subtracting is 20 The result of multiplying is 300 The result of dividing is 3.0 Enter value a: -28 Enter value b: 14 The result of adding is -14 The result of subtracting is -42 The result of multiplying is -392 The result of dividing is -2.0 Hint: Exercises of Week import java.util.Scanner; public class CaculateTwoValues{ public static void main(String [] args) { caculateValues(); } static void caculateValues() { int a, b; int resulta, results, resultm; float resultd; Scanner sc = new Scanner(System.in); System.out.print("Enter value a:"); a = sc.nextInt(); System.out.print("Enter value b:"); b = sc.nextInt(); / / / {write you code here ///} System.out.println("The result of adding is " + resulta); System.out.println("The result of subtracting is " + results); System.out.println("The result of multiplying is " + resultm); System.out.println("The result of dividing is " + resultd); } } Exercises of Week Exercise 6: In this exercise, complete the function that "returns a value" When you call this function, it should calculate the area of the triangle using Heron's formula and return it Heron's formula: Area = (s*(s-a)*(s-b)*(s-c)) 0.5 where s = (a+b+c)/ Hint: public class AreaTriangle{ public static void main(String [] args){ double a; a = triangleArea(3, 3, 3); System.out.println("A triangle with sides 3,3,3 has an area of:" + a); a = triangleArea(3, 4, 5); System.out.println("A triangle with sides 3,4,5 has an area of:" + a); a = triangleArea(9, 9, 9); System.out.println("A triangle with sides 9,9,9 has an area of:" + a ); } / / Write your code here public static double triangleArea(int a, int b, int c){ / / the code in this function computes the area of a triangle whose sides have lengths a, b, and c / / ^ after computing the area, "return" it } } Exercises of Week Exercise 7: Write Java program to allow the user to input temperature in Celsius and convert it into Fahrenheit Enter a temperature in Celsius: 25 The temperature in Fahrenheit is: 77 Enter a temperature in Celsius: The temperature in Fahrenheit is: 32.0 Enter a temperature in Celsius: -25 The temperature in Fahrenheit is: -13.0 Hint: import java.util.Scanner; public class Challenge{ public static void main(String [] args){ System.out.println("Enter a temperature in Celsius: "); Scanner scanCelsius = new Scanner(System.in); double Fahrenheit = 0; / / / {write your code here ///} System.out.println("The temperature in Fahrenheit is: " + Fahrenheit); } } Exercises of Week Exercise 8: Write Java program to allow the user to input the amount of deposit, yearly interest rate (percentage), and income tax(percentage) Then the program will calculate the amount of interest that the person earns in the year See the example output below: The amount of deposit: 1000 Yearly interest rate: 7.5% Income tax rate: 4% The amount of interest earned in the year: 72.0 Hint: import java.util.*; public class CaculateInterest{ public static void main(String[] args){ caculateInterest(); } static void caculateInterest(){ float amount_dep, rate, tax, interest_earned, tax_amount; Scanner sc=new Scanner(System.in); / / {Write your code here // } }

Ngày đăng: 23/10/2016, 14:45

TỪ KHÓA LIÊN QUAN