java learning_Java Learning (20110802)

Mainly focus on the basic entrance.

Introduction, Pseudo Code, Simple java code

=======================

Pseudo Code

Question: you put $10000 into a bank account that earns 5 % interest per year. How many years does it take for the account balance to be double the original?

Start with a year value 0 and a balance of $10000

year Balance

0 10000

Repeat the following steps with the balance is less than 20000

Add 1 to the year value.

Multiply the balance value by 1.05 (a 5 percent increase)

year balance

0 10000

1 10500

… …

14 19799.32

15 20789.28

Report the final year value as the answer

Here are the kinds of pseudo code statements that we will use in this book:

• Use statements such as the following to describe how a value is set or changed:

total cost = purchase price + operating cost

or

Multiply the balance value by 1.05.

or

Remove the first and last character from the word.

• Describe decisions and repetitions as follows:

If total cost 1 < total cost 2

While the balance is less than $20,000

For each picture in the sequence

Use indentation to indicate which statements should be selected or repeated.

For each car

operating cost = 10 x annual fuel cost

total cost = purchase price + operating cost

Here, the indentation indicates that both statements should be executed for each car.

• Indicate results with statements such as

Choose car1.

Report the final year value as the answer.

The exact wording is not important. What is important is that the pseudo code describes a sequence of steps that is

• Unambiguous

• Executable

• Terminating

A sequence of steps that is unambiguous, executable, and terminating is called an algorithm.

Question: Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give an algorithm to compute the monthly charge for a given number of minutes.

Answer before read “How to 1.1”:

Total monthly charge = Fixed charge + Additional charge

Fixed charge = $29.95

If minute Additional charge = 0

Else

Additional charge = (Tax+1) x (minute-300) x $0.45

Minute Total Monthly Charge ($)

0 29.95

1 29.95

2 29.95

… 29.95

300 29.95

301 29.95 + 1.125 x (301-300) x 0.45

302 29.95 + 1.125 x (302-300) x 0.45

… …

Y 29.95 + 1.125 x (y-300) x 0.45

Report the total monthly charge as the answer

Answer after read “How to 1.1”:

Step 1: determine the inputs and outputs

Input:

. Called Minute: how many minutes I have called in this month

Output:

. The Total Monthly Charge

Steps 2: break down the problem into smaller tasks

The Monthly Charge is Fixed Charge plus Additional Charge.

Fixed charge:

Fixed charge is a given number (in this case: $29.95)

For Additional Charge:

If Called Minute Additional charge = 0

Else

Additional charge = (Tax+1) x (Called Minute-Specific number) x Unit Price

(in this case, Tax = 0.125, Specific number = 300, Unit Price = $0.45)

For example, if you called 10 minutes this month, the Monthly Charge is $29.95 + 0 = $29.95; if you called 302 minutes this month, the Monthly Charge is $29.95 + 1.125 x (302-300) x $0.45 = $30.9625

Round the Result:

Round the result into 2 digits, decimal.

For example, if result is 2, round to 2.00; if result is $30.9625, round to $30.96; if result is $30.9655, round to $30.97.

Step 3: Describe each subtask in pseudo code

Monthly Charge = Fixed Charge + Additional Charge

Here’s the algorithm for the Monthly Charge:

If Called Minute Monthly Charge = Fixed Charge

Else

Monthly Charge = Fixed Charge + (Tax+1) x (Called Minute-Specific number) x Unit Price

Return Round(Monthly Charge)

Steps 4: Test my pseudo code

Use below sample values:

Specific number = 300

Fixed Charge = 29.95

Tax = 0.125

Called Minute = 302

Unit Price = 0.45

Here’s the calculation:

Monthly Charge = Fixed Charge + Additional Charge = 29.95 + (0.125+1) x (302 – 300) x 0.45

= $30.9625

Round(Monthly Charge) = $30.96

So I need pay $30.96 this month.

Summary of learning Objectives

Describe the process of translating high-level languages to machine code.

• Generally, machine code depends on the CPU type. However, the instruction set of the Java virtual machine (JVM) can be executed on many CPUs.

• Because machine instructions are encoded as numbers, it is difficult to write programs in machine code.

• High-level languages allow you to describe tasks at a higher conceptual level than machine code.

• A compiler translates programs written in a high-level language into machine code.

Describe the building blocks of a simple program and the structure of a method call.

• Classes are the fundamental building blocks of Java programs.

• Every Java application contains a class with a main method. When the application starts, the instructions in the main method are executed.

• Each class contains definitions of methods. Each method contains a sequence of instructions.

• Use comments to help human readers understand your program.

• A method is called by specifying an object, the method name, and the method parameters.

• A string is a sequence of characters enclosed in quotation marks.

Classify program errors as syntax and logic errors.

• A syntax error is a violation of the rules of the programming language. The compiler detects syntax errors.

• A logic error causes a program to take an action that the programmer did not intend. You must test your programs to find logic errors.

Classes, Objects, and Methods Introduced in this Chapter

java.io.PrintStream

print

println

java.lang.System

out

HelloSwiss.java

package package01;

public class HelloSwiss {

public static void main(String[] args)

{

//Hello world

System.out.println(“hello swiss”);

System.out.println(“3+4″);

System.out.println(3+4);

System.out.println(3+”4″);

NamePrinter(“Allen”);

CapitalAllen();

Print3Lines(“Hans”, “Martin”, “Christian”);

P18(10);

}

static void NamePrinter(String sName)

{

//display my name in a box

System.out.println(“+—–+”);

System.out.println(“|”+sName+”|”);

System.out.println(“+—–+”);

}

static void CapitalAllen()

{

System.out.println(” x x x x x x x x”);

System.out.println(” x x x x x xx x”);

System.out.println(” x x x x x x x x x x x”);

System.out.println(” x x x x x x x”);

System.out.println(“x x x x xxxxx x x x x x”);

}

static void Print3Lines(String s1, String s2, String s3)

{

//display my name in a box

System.out.println(“My friends are:\n”+s1+”\n”+s2+”\n”+s3+”\n”);

}

static void P18(int i)

{

int a =1;

int sum = 0;

while (a {

sum = sum + a;

a++;

}

System.out.println(sum);

}

}

———

hello swiss

3+4

7

34

+—–+

|Allen|

+—–+

x x x x x x x x

x x x x x xx x

x x x x x x x x x x x

x x x x x x x

x x x x xxxxx x x x x x

My friends are:

Hans

Martin

Christian

55

DialogViewer.java

package package01;

import javax.swing.JOptionPane;

import java.net.URL;

import javax.swing.ImageIcon;

public class DialogViewer {

public static void main(String[] args) throws Exception

{

PrintMyName(“Allen Guo”);

PrintInputName();

GreetImage();

}

static void PrintMyName(String str)

{

JOptionPane.showMessageDialog(null, “Hello, “+ str +”!”);

System.exit(0);

}

static void PrintInputName()

{

String name = JOptionPane.showInputDialog(“What’s your name?”);

//System.out.println(“Good morning, “+name+”!”);

JOptionPane.showMessageDialog(null,”Good morning, “+name+”!”);

System.exit(0);

}

static void GreetImage() throws Exception

{

URL imageLocation = new URL(“http://ess.ch/blog/wp-content/uploads/2011/08/BigJava-242×300.jpg”);

JOptionPane.showMessageDialog(null,”\rHello, I am tiger!”, “Tiger”, JOptionPane.PLAIN_MESSAGE, new ImageIcon(imageLocation));

System.exit(0);

}

}

————————-

Question: why stopped after completing the first method?

PrintMyName(“Allen Guo”);

PrintInputName();

GreetImage();

Answer:

System.exit(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值