Java OBJECT-ORIENTED8/5

Source: Internet
Author: User
Tags lenovo

Package chapter1.class;
/**
* Make a simple address book.
* @author LENOVO
*
*/
public class Classdemo_01 {
String name;//Name
String phonenum;//Contact Number
String Email;//e-mail
String address;//Home Address
Use the constructor of the class to assign a value to its member variable
Public classdemo_01 (String name,string phonenum,string email,string address) {
This.name=name;
This.phonenum=phonenum;
This.email=email;
this.address=address;
}
/**
* A method for querying information is required.
*/
public void Printmessage () {
System.out.println ("Name:" +name);
SYSTEM.OUT.PRINTLN ("Contact number:" +phonenum);
System.out.println ("e-mail:" +email);
System.out.println ("Home Address:" +address);
}
public static void Main (String[]args) {
/**
* Create an instantiated object of the class with the New keyword and assign it a value ...
*/
classdemo_01 cd=new classdemo_01 ("Xiaohong Zhang", "13343235564", "[email protected]", "Beijing Changping District Shahe Town No. 123th");
Cd.printmessage ();
}

}

Package chapter1.class;
/**
* Simple Length unit conversion
* @author LENOVO
*
*/
Import Java.util.Scanner;

public class Length {

Double num;//numeric value
String units;//Unit
String targetunits;//Target Unit
Double result;//The result of the final conversion
String str= "";
Assigning values to member variables using construction methods
Public Length (double num,string units,string targetunits) {
This.num=num;
This.units=units;
This.targetunits=targetunits;

}
/**
* Method of conversion between units
*/
Public String unitconversion () {
inches------> centimeters
if (Units.equals ("inch") &&targetunits.equals ("cm")) {
result=num*2.54;
str=num+ "inch =" +result+ "cm";
}
CM------> inches
else if (units.equals ("cm") &&targetunits.equals ("inch")) {
result=num* (1/2.54);
str=num+ "cm =" +result+ "inch";
}
Rice------> cm
else if (units.equals ("M") &&targetunits.equals ("cm")) {
result=num*100;
str=num+ "m =" +result+ "cm";
}
CM------> m
else if (units.equals ("cm") &&targetunits.equals ("M")) {
result=num* (1.0/100);
str=num+ "cm =" +result+ "M";
}
inches----> Feet
else if (units.equals ("inch") &&targetunits.equals ("feet")) {
result=num* (1/12.0);
str=num+ "inch =" +result+ "foot";
}
Feet-----> inches
else if (units.equals ("Foot") &&targetunits.equals ("inch")) {
result=num*12;
str=num+ "foot =" +result+ "inch";
}
Feet-----> M
else if (units.equals ("Feet") &&targetunits.equals ("M")) {
result=num* (1/3.2809);
str=num+ "foot =" +result+ "M";
}
M-----> Feet
else if (units.equals ("M") &&targetunits.equals ("feet")) {
result=num*3.2809;
str=num+ "m =" +result+ "feet";
}
return str;
}
/**
* Main method
*/
public static void Main (String[]args) {
System.out.println ("the conversion between length units is as follows:");
System.out.println ("\ n");
Scanner scanner=new Scanner (system.in);
System.out.println ("Please enter a number:");
Double num1=scanner.nextdouble ();
System.out.println ("Please enter the original unit:");
String Str1=scanner.next ();
System.out.println ("Please enter the unit to be converted into:");
String Str2=scanner.next ();
Length cd=new length (NUM1,STR1,STR2);
System.out.println (Cd.unitconversion ());
}



}

Package chapter1.class;

Public class Truckdivers {
 string name;//defines the name of the truck driver
 //truckdivers construction method
 public Truckdivers (String name) {
  this.name=name;
 }
 //the speed of the truck by means of truck-based classes.
 public void Drive (int num) {
  truck tr=new Truck ();
  system.out.print (name);
  tr.speed (num);
 }
 public static void Main (String[]args) {
  truckdivers td=new truckdivers ("Zhang San");
  truckdivers td1=new truckdivers ("John Doe");
  td.drive (80);
  system.out.println ("\ n");
  td1.drive (38);
 }

}
Class truck{
public int wheelnum;//Wheel number of car
public int weight;//weight of car
public int drivespeed;//Driving speed
public void speed (int speed) {
if (speed>=60) {
System.out.println ("Speeding, easy to cause traffic accidents!") ");
drivespeed=40;//if the speed is too fast, it automatically travels at a speed of 40 yards.
System.out.println ("Your current speed is" +drivespeed);
}
else{
System.out.println ("Driving in safe travel speed!") ");
Drivespeed=speed;
System.out.println ("Your current speed is" +drivespeed);
}
}
}

Java OBJECT-ORIENTED8/5

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.