Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 2       Name __________________ 
Introduction  
The purpose of this lab is to introduce you to the fundamental concepts of computer 
programming using a graphical approach. Java applets were introduced in Chapter 1. It 
was the java applet that made Java famous in the early 1990s. The applet opened a new 
programming paradigm, one that could be launched using the Internet.  
Compiling and Executing a Java Applet 
Using a text editor, open the file "HelloApplet.java". The java source code is shown 
below in figure 1.  
import javax.swing.*; 
import java.awt.*; 
 
public class HelloApplet extends JApplet 
{ 
 public void paint(Graphics canvas) 
 { 
  canvas.drawString("Welcome to Java Programming!", 25,25); 
 } 
} 
Figure 1 
 
To invoke the Java compiler and Applet Viewer using TextPad,  
1. Select Compile Java from the Tools menu or press Ctrl-1 to invoke the Java 
compiler. If there were no errors, a class file is produced.  
2. Select Run Java Applet from the Tools menu or press Ctrl-3 to invoke the 
applet viewer. The running applet is displayed in Figure 2 below.  
Modifying an Existing Java Program  
1. Add source code to HelloApplet.java to display your name under the text 
"Welcome to Java Programming!".  
2. Compile and run your modified program using the steps outlined above in the 
section entitled "Compiling and Executing a Program."  
3. Show your lab instructor or assistant your working program.  
Resolving Syntax Errors 
1. Using a text editor, open the Java source code file named ErrorApplet.java.  The 
source code is shown in Figure 2 below.  The working applet is shown in Figure 3 
below. 
2. This program contains several syntax errors as well as one logic error.  Compile 
the program and observe the error messages. 
3. Resolve the errors in the program.  The compiler will produce the file 
ErrorApplet.class that contains the executable byte code when all the errors are 
resolved. 
4. Show your lab instructor or assistant your working program. 
import java.awt.*; 
 
public class ErrorApplet extends JApplet 
{ 
 public void paint(Graphics canvas) 
 { 
  canvas.drawLine(25, 25, 125,25) 
  canvas.drawString(A Graphical Applet!, 25, 50); 
  canvas.drawLine(25, 25, 125, 75); 
 } 
} 
Figure 2. 
 
 
Figure 3. 
Lab 3       Name __________________ 
Introduction  
The purpose of this lab is to introduce you to variables and expressions to store and 
compute values, keyboard input, problem solving using algorithms, and comments to 
document source code.  
Variables and Expressions 
Using a text editor, open the file "Average3.java." The Java source code is shown below 
in Figure 1.  
public class Average3 
{ 
 public static void main(String[] args) 
 { 
  int num1 = 75; 
  int num2 = 137; 
 
  double average = (num1 + num2)/2; 
 
  System.out.print("The average of " + num1); 
  System.out.println(" and " + num2 + " is " + average); 
 } 
} 
Figure 1. 
Compile and run "Average3.java." and observe the output. The program declares three 
variables; two variables hold integer numbers that have been provided within the 
program. and one variable of type double is declared to store the result of the calculation. 
The program then displays the average of the two numbers.  
Modify the program as follows:  
1. Create an integer variable named num3. 
2. Assign the value of 265 to num3. 
3. Modify the average calculation to include num3. 
4. Modify the output statements as appropriate. 
5. Compile and run the program.  If you encounter any errors (syntax or logic) that 
you cannot resolve, ask the lab instructor or assistant for help. 
6. When your program works correctly, show the running program to the lab 
instructor or lab assistant. 
Algorithms and Pseudocode-Problem Solving Tools  
An algorithm is a step-by-step method of solution. Algorithms are used in programming 
to break a complex problem down into simpler steps that can be easily accomplished. 
Pseudocode is a tool that is used to define the logic of the algorithm. Pseudocode is 
simply English-like statements that describe the algorithm. A programmer uses 
pseudocode to create source code.  
Below is a problem statement.  In the space provided, write pseudocode statements to 
solve the problem. 
Problem: 
A program to compute the square of an integer is needed. The program should allow the 
user to enter an integer, compute the square. and then display the result.  
Pseudocode:  
 
 
 
 
1. Write the source code to implement your pseudocode. Test your working program 
with the following values: 2, 10, and -4. Record your results below.  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2. When your program is working correctly, show it to the lab instructor or lab 
assistant.  
 
Java’s String Class  
Using a text editor, open the file "StringDemo.java." The Java source code is shown 
below in Figure 2.  
import java.util.*; 
 
public class StringDemo 
{ 
 public static void main(String[] args) 
 { 
  Scanner keyboard = new Scanner(System.in); 
  String input; 
 
  System.out.print("Enter a string: "); 
  input = keyboard.next(); 
 
  System.out.println("You entered: " + input); 
 } 
} 
Figure 2. 
Modify the program as follows:  
1. Insert a line of code that calculates the length of the string the user entered and 
displays it. Insert your line of code after: System.out.println("You entered: " + 
input);  
2. Show your running program to the lab instructor or lab assistant.  
 
Comments  
Comments allow the programmer to document program logic within the source code.  
1. Add comments to document the logic of the program you created in the section 
entitled "Algorithms and Pseudocode--Problem-Solving Tools." From this point 
on, each of your programs should contain basic comments. At the beginning of 
each program, include your name, your course and section number, the date, and a 
brief description of the program.