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

Lý thuyết lập trình JAVA 1

7 431 1

Đ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 34,58 KB

Nội dung

Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA Lý thuyết lập trình JAVA

Exercises of Week Getting Started - Write your First Hello-world Java Program Let us begin by writing our first Java program that prints a message "Hello, world!" to the display console, as follows: Hello, world! Step 1: Write the Source Code: Enter the following source codes using a programming text editor (such as TextPad or NotePad++ for Windows or edit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans - Read the respective "How-To" article on how to install and get started with these IDEs) Do not enter the line numbers (on the left panel), which were added to help in the explanation Save the source file as "Hello.java" A Java source file should be saved with a file extension of ".java" The filename shall be the same as the classname - in this case "Hello" /* * First Java program, which says "Hello, world!" */ public class Hello { // Save as "Hello.java" public static void main(String[] args) { System.out.println("Hello, world!"); // print message } } Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable bytecode "Hello.class" using JDK compiler "javac" Start a CMD Shell (Windows) or Terminal (UNIX/Linux/Mac) and issue this command: prompt> javac Hello.java where javac is the name of JDK compiler There is no need to explicitly compile the source code under IDEs (such as Eclipse or NetBeans), as they perform incremental compilation implicitly Step 3: Run the Program: Run the program using Java Runtime "java", by issuing this command: prompt> java Hello Exercises of Week Hello, world! Take note that the Run command is "java Hello" without the ".class" extension On IDEs (such as Eclipse or NetBeans), right-click on the source file and choose "Run As " "Java Application" EXERCISES OF WEEK Exercise 1: Try the following program and explain the output produced: 10 11 12 /* Test System.out.println() and System.out.print() */ public class PrintTest { // Save as “PrintTest.java” public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); // Print a empty line System.out.print("Hello, world!"); // Cursor stayed after the printed string System.out.println("Hello,"); System.out.print(" "); // Print a space System.out.print("world!"); System.out.println("Hello, world!"); } } Exercise 2: Write a Java program to display the screen: Fullname, DOB, class name of you Hint: public class FullName{ //Save as “FullName.java” public static void main(String [] arg){ ///{ write your code here ///} } } Exercises of Week Exercise 3: Write a program that will print your initials to standard output in letters that are nine lines tall Each big letter should be made up of a bunch of *'s For example, if your initials were "DJE", then the output would look something like: ****** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***** ************* ** ** ** ** ** ** ** ** ** ** **** Hint: public class PrintWord{ //Save as “PrintWord.java” public static void main(String [] arg){ ///{ write your code here ///} } } ********** ** ** ** ******** ** ** ** ********** Exercises of Week Exercise 4: Complete the given Java program such that it multiplies the two inputted numbers Input: Output: Input: Output: Hint: import java.util.Scanner; public class Challenge{ //Save as “Challenge.java” public static void main(String args[]){ Scanner scanner=new Scanner(System.in); int product=0; System.out.println("Enter the 1st number:"); int num1=scanner.nextInt(); System.out.println("Enter the 2nd number:"); int num2=scanner.nextInt(); ///{ write your code here ///} System.out.println("The product of the two numbers is:"+product); } } Exercises of Week Exercise 5: Write a java program to ask the user for his/her name, age, and salary (double) Follow the input/output format Following conversation should be displayed as output on screen, where you will enter the values of name,age and salary Suppose your inputs are: Tom 22 500 Expected Output: Hello What is your name? Hi Tom! How old are you? So you're 22 eh? That's not old at all! How much you make John? Exercise fjdkthat's per hour and not per year! LOL! 500.0! I5:hope Hint: import java.util.Scanner; public class NameAgeSalary{ //Save as “NameAgeSalary.java” public static void main(String args[]){ Scanner scanner=new Scanner(System.in); ///{Write you code here ///} } } Exercises of Week Exercise 6: Ask the user for several pieces of information, and display them on the screen afterward as a summary • • • • • • first name last name grade (classification) student id number login name GPA (0.0 to 4.0) For example: Input: First name: Helena Last name: Bonham-Carter Grade (9-12): 12 Student ID: 453916 Login: bonham_453916 GPA (0.0-4.0): 3.73 Output: Login:bonham_453916 ID:453916 Name:Bonham-Carter; Helena GPA:3.73 Grade:12 Exercises of Week Hint: import java.util.Scanner; public class Information{ //Save as “Information.java” public static void main(String args[]){ Scanner scanner=new Scanner(System.in); System.out.println("Please enter the following information so I can sell it for a profit!:"); System.out.print("First name: "); String fn = scanner.next(); System.out.print("Last name: "); String ln = scanner.next(); System.out.print("Grade (9-12): "); int grade = scanner.nextInt(); if (grade < || grade > 12) { System.out.print("Invalid Grade"); } System.out.print("Student ID: "); String id = scanner.next(); System.out.print("Login: "); String login = scanner.next(); System.out.print("GPA (0.0-4.0): "); double gpa = scanner.nextDouble(); if (gpa < || gpa > 4) { System.out.println("Invalid GPA"); } ///{write you code here ///} } }

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

TỪ KHÓA LIÊN QUAN

w