代写范文

留学资讯

写作技巧

论文代写专题

服务承诺

资金托管
原创保证
实力保障
24小时客服
使命必达

51Due提供Essay,Paper,Report,Assignment等学科作业的代写与辅导,同时涵盖Personal Statement,转学申请等留学文书代写。

51Due将让你达成学业目标
51Due将让你达成学业目标
51Due将让你达成学业目标
51Due将让你达成学业目标

私人订制你的未来职场 世界名企,高端行业岗位等 在新的起点上实现更高水平的发展

积累工作经验
多元化文化交流
专业实操技能
建立人际资源圈

Programming_Notes

2013-11-13 来源: 类别: 更多范文

1 LESSON 2:Data Types, Variables and Java Packages 1. Data Types I think coming from C++ to java is one of the easiest programming language transformations. You get most of the things are what you already know, and thus you wont have a hard time. Java supports 8 primitive data types;  byte  short  int  long  float  double  boolean  char Unlike in C++, the data type ‘String’ is a class in Java. It contains numerous methods that can be used to handle and manipulate strings in Java. 2. Variables Java programming language has 4 kinds of variables. i) Instance variables ii) Class variables iii) Local variables iv) Parameters Coming from C++, (iii) and (iv) should be very familiar to you. The good thing is, everything you knew about them in C++ is also true in Java! Naming Variables Just like in C++, there are various housekeeping rules and conventions that you have to follow when you are declaring variables in java. i) Variables are case sensitive. ii) A variable name can be any length of Unicode1 letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. 2 iii) White space is not permitted. iv) Subsequent characters may be letters, digits, dollar signs, or underscore characters. When choosing a name for your variables, use full words instead of cryptic abbreviations. E.g. studentID instead of stdid, sid or stdntid ; grade instead of grd, gd or grde, etc. This will make it easier to understand what your variables are meant for. v) The name that you choose for your variable name must not be a keyword2. vi) If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word, e.g. studentGrade, getArea, getTotal, calculateVolume, studentName, etc. Declaring Variables Just as you did it in C++, variable declarations in java follow the same syntax. i.e. [data type] [variable name] ; //variable declaration or [data type] [variable name] = value ; // variable declaration and initialization Examples; int length; int width; int area = 0; int volume = 0.00; 3. Java Packages The power of Java lies in its rich collection of predefined classes that a programmer can reuse rather than ‘reinventing the wheel’. These classes are grouped into packages. Collectively, the java packages are referred to as the Java class library, or the Java Application Programming Interface (Java API). For the java version we are using, you may check the complete listing and explanation of the Java API from http://java.sun.com/docs/5.0/api. 3 Actually, to become a java guru, you are just required to know where to find a particular class (i.e in which package to find it), and to know the class’ methods, and how they can be used. To identify and use any predefined class in Java, programmers use import declarations. This is similar to the preprocessor directives you met in C++ (e.g. #include ). Assuming that you want to write a program that reads numbers from the keyboard, adds them up and displays the sum. In C++, you used ‘cin’ followed by the input stream operarators(>>). E.g. cin>>number1; Remember that ‘cin’ was defined in the iostream header file, and thus the reason whey you had to include it at the beginning of your C++ program. In a similar manner, if you want to read numbers from the keyboard in Java, all you need to know is which class defines the methods to handle that, and in which package it is found. With that, now you can import that class in your program, and then be able to use its methods. To use a certain method that belongs to a certain class in Java, you declare a variable whose [data] type is the name of that class. You will understand this in a moment. Line 8 of the program below is a variable declaration. The variable name is ‘input’ and its of type Scanner. The variable is initialized to ‘new Scanner (System.in)’. This expression creates a Scanner object that reads data typed by the user at the keyboard. In this case, System.in enables java applications to read information typed by the user. In the program below, lines 13 and 15 are used to read numbers from the keyboard, which are assigned to the already declared variables. Line 16 is then used to get the sum of the two numbers. Sample Program. // Program to read two numbers from the keyboard and display their sum 1 import java.util.Scanner; // program uses class Scanner 2 public class Addition 3 { 4 // main method begins execution of Java application 5 public static void main( String args[] ) 6 { 7 // create Scanner to obtain input from command window 8 Scanner input = new Scanner(System.in ); 9 int number1; // first number to add 10 int number2; // second number to add 11 int sum; // sum of number1 and number2 12 System.out.print( "Enter first integer: " ); // prompt 13 number1 = input.nextInt(); // read first number from user 4 14 System.out.print( "Enter second integer: " ); // prompt 15 number2 = input.nextInt(); // read second number from user 16 sum = number1 + number2; // add numbers 17 System.out.printf( "Sum is %d\n", sum ); // display sum 18 } // end method main 19 } Exercise 1. For all the 8 primitive data types supported by Java Programming language, identify the possible range of values and the default value. Use the below format; Data Type Possible Range Default Value 2. Write a java program that;  Prompts the user to enter three numbers,  Performs the following mathematical calculations on three numbers; Addition, Subtraction and Multiplication. NB: Display the answers in at least two decimal places. 3. Do Exercise 2.28 (Java How To Program, 6th Edition or 7th Edition)
上一篇:Pttls_Assignment_4_-_Ground_Ru 下一篇:Professional_Knowledge_and_Abi