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

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

7 432 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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

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

Trang 1

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"

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:

1

2

3

4

5

6

7

8

/*

* 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 }

}

Trang 2

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"

Exercise 1: Try the following program and explain the output produced:

1

2

3

4

5

6

7

8

9

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 1

Trang 3

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

///}

Trang 4

Exercise 4 : Complete the given Java program such that it multiplies the two inputted

numbers

Input:

1

2

Output:

2

Input:

2

3

Output:

6

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

Trang 5

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:

Exercise 5 : fjdk

Hint:

Hello What is your name?

Hi Tom! How old are you?

So you're 22 eh? That's not old at all!

How much do you make John?

500.0! I hope that's per hour and not per year! LOL!

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

///}

Trang 6

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

Trang 7

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 < 9 || 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 < 0 || 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