After-school exercises for Java classes and objects

Source: Internet
Author: User

Java classes and objects after-school practice programming (JAVA2 Practical tutorial P111)

This chapter of the content of self-study, in the course of other exercises after doing teaching materials (as long as their careful) are no problem, but in the last computer programming problems when the problem arises, in the reading of the topic when you can feel that the problem is not difficult (indeed it is not difficult), I also quickly wrote out, but run the program, The results have been incorrect, the final change to change is not correct, the idea is right, step-by-stage implementation of the time also feel that there is a place out of the problem but is not out, and finally the textbook in front of the example of repeated look at the contrast, finally found the problem:

SYSTEM.OUT.PRINTLN ("CPU speed:" +CPU); SYSTEM.OUT.PRINTLN ("Capacity of the hard disk:" +HD);

In the PC class appeared this error, find the reason to think this error is good low, but summed up is this chapter knowledge not pass, to this chapter content inside a lot of relationship is vague and miscellaneous, still not clear, so I still need to spend time on this chapter above. Remind yourself to keep the notes in mind when you're doing the code again. In general this week self-study This chapter of the content is still very fruitful. Refuel yourself! Insist!

public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub       CPU CPU =new CPU ();      Create a CPU Object       cpu.setspeed (2200);       The CPU sets the speed to 2200       harddisk disk = new harddisk ();     Create a Disk object       Disk.setamount ($);       Disk will be set to amount PC       pc =new pc ();          Create a PC object       pc.setcpu (CPU);       Pc.setharddisk (disk);   Pc.show ();      }}

  

public class PC {CPU CPU = new CPU (); Harddisk HD = new harddisk (); void SetCPU (CPU c) {this.cpu = C;} public void Setharddisk (harddisk h) {HD = h;}    public void  Show () {System.out.println ("CPU speed:" +cpu.speed); SYSTEM.OUT.PRINTLN ("Capacity of the hard disk:" +hd.amount);}}

  

public class CPU {    int speed;    public void setspeed (int m) {        this.speed = m;    }    int GetSpeed () {        return speed;  }}
public class Harddisk {int amount;public void setamount (int m) {this.amount=m;} int Getamount () {return amount;}}

Output Result:

CPU Speed: 2200 HDD Capacity: 200

  

After-school exercises for Java classes and objects

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.