Java program code writes and writes the tree data structure job

Source: Internet
Author: User
Tags instance method

Java program code writes and writes the tree data structure job
Experiment Three: Java Object-oriented programming
First, the purpose and requirements of the experiment
1, understand how the Java language embodies the basic idea of object-oriented programming;
2, master the declaration of the class and the creation of the object;
3. Understand the attributes of the class's member variables and member methods, and the use of the class's construction methods.
4, master the difference between the class variable and the instance variable, and the class method and the instance method.
Ii. contents of the experiment
1, programming simulation of two villages together with a forest. Write a village class that has a static INT member variable TREEAMOUNT to simulate the number of trees in the forest. Create two villages in the main class MainClass method, one village changes the value of Treeamount, and the other village looks at the value of Treeamount. The program template is as follows, replacing "code" with Java program code.
Requirements: Compile, run the program, and add the complete source program and the output of the program is written in the experimental report.
Mainclass.java
public class MainClass {
public static void Main (String args[]) {
Village Zhaozhuang,majiahezhi;
Zhaozhuang = new Village ("Zhao Zhuang");
Majiahezhi = new Village ("Ma Jia Majiahezi");
zhaozhuang.peoplenumber=100;
majiahezhi.peoplenumber=150;
"Code 1"//access Treeamount with class name village and assign a value of 200
int Lefttree =village.treeamount;
SYSTEM.OUT.PRINTLN ("There are +lefttree+" trees in the forest);
"Code 2"//zhaozhuang calls treeplanting (int n) and passes a value of 50 to the parameter
Lefttree = "Code 3"//majiahezhi Call Looktreeamount () method to get the number of trees
SYSTEM.OUT.PRINTLN ("There are +lefttree+" trees in the forest);
"Code 4" Majiahezhi calls Felltree (int n) and passes a value of 70 to the parameter
Lefttree = Village.looktreeamount ();
SYSTEM.OUT.PRINTLN ("There are +lefttree+" trees in the forest);
System.out.println ("The population of the Zhao Zhuang:" +zhaozhuang.peoplenumber);
Zhaozhuang.addpeoplenumber (12);
System.out.println ("The population of the Zhao Zhuang:" +zhaozhuang.peoplenumber);
System.out.println ("The population of Ma Jia Majiahezi:" +majiahezhi.peoplenumber);
Majiahezhi.addpeoplenumber (10);
System.out.println ("The population of Ma Jia Majiahezi:" +majiahezhi.peoplenumber);
}
}
Village.java
Class Village {
static int treeamount; Simulating the number of trees in the forest
int peoplenumber; Number of villages
String name; The name of the village
Village (String s) {
name = S;
}
void treeplanting (int n) {
Treeamount = Treeamount+n;
System.out.println (name+ "Tree Planting" +n+ ");
}
void Felltree (int n) {
if (treeamount-n>=0) {
Treeamount = Treeamount-n;
System.out.println (name+ "Cutting Tree" +n+ ");
}
else {
System.out.println ("No trees can be cut");
}
}
static int Looktreeamount () {
return treeamount;
}
void Addpeoplenumber (int n) {
Peoplenumber = Peoplenumber+n;
System.out.println (name+ "added" +n+ "person");
}
}

2, programming to create a box class (box), in the box class to define three variables, representing the length of the box (length), Width (width) and height (heigth), and then define a method void Setbox (int l, int w, int h) Initialize the three variables, and then define a method int volume () to calculate the volume of the box. Finally, a box class object B is created in main class Ky3_2 's main () method, first by invoking the Setbox () method of object B to set the length, width and height of the box, and then by invoking the volume () method of object B to calculate the volume of the box of the given size and output the result.
Requirements: The source code of the program, the results of the operation is written on the experimental report.

3, write a Java application main, in the main class of the major method to declare the type of the product number of the int variable amount and the product unit price of the float variable, and the total value of the entire product float type variable sum. Use the Scanner object call method to allow the user to enter a value from the keyboard for the amount, the price variable, and then the program calculates the total value of the product and outputs the value of the amount. (The code template below, replace "code" with Java program code) can see the textbook P30 practice.

Import Java.util.Scanner;
public class Ky3_3 {
public static void Main (String args[]) {
Scanner reader=new Scanner (system.in);
int amount = 0;
Float price=0,sum=0;
System.out.println (Enter the product quantity (return confirmation): ");
"Code 1"//Amount assignment from keyboard
System.out.println ("Enter Product unit Price (return confirmation):");
"Code 2"//value
sum = Price*amount from the keyboard;
System.out.printf ("Quantity:%d, Unit Price:%5.2f, total value:%5.2f", amount,price,sum);
}
}
4, establish the Ky3_4.java file, through this program to understand the scope of use of variables, the source code is as follows.
public class Ky2_2 {
static int i=10;
public static void Main (String args[]) {
{
int k=10;
System.out.println ("i=" +i);
System.out.println ("k=" +k);
}
System.out.println ("i=" +i);
System.out.println ("k=" +k);
}
}
Request:
1) Compile Ky2_2.java to see if this program will go wrong? Why compile errors occur, please give reasons in the test report!
2) Modify the program above to allow it to run correctly.
3) Write the modified source program and the output of the program in the experimental report.
Thinking: What is the problem with the program?
Three, experimental equipment and environment
Windows7 and above system, installation ECLIPSE+JDK
Four, experimental process and results
v. Summary of the experiment
http://www.daixie0.com/contents/9/1298.html

Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:

C/c++/c# Write

Java Write generation

It generation

Python writes

Tutoring Programming Jobs

The MATLAB Generation writes

Haskell writes

Processing Write

Linux Environment Setup

Rust Generation Write

Data Structure assginment Data structure generation

MIPS Generation Writing

Machine Learning Job Writing

Oracle/sql/postgresql/pig database Generation/Generation/Coaching

Web development, Web development, Web site jobs

Asp. NET Web site development

Finance insurace Statistics Statistics, regression, iteration

Prolog write

Computer Computational Method Generation

Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected]

: Codinghelp

Java program code writes and writes the tree data structure job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.