简体   繁体   中英

java calling methods and objects

my dear Friends I am new to this java ..so plz help me thank u in advance.. Now i am workin on an examples to understand the concepts of java's objects, classes method in detail. With all the knowledge i have i tried this example. . but i am unable to accomplish the task.. . Can any one plz give me suggestions of corrected coding..

I also wanted to know Which s the best site for learning JAVA (like MSDN for .NET ) Thank u in advance

Question:To Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional attributes: loading capacity( 100 tons…).Add a behavior to change the color and loading capacity. Display the updated truck details.

Codings:

import java.io.*;

class Vehicle
{
    String VehicleNo;
    String Model;
    String Manufacturer;
    String Color;


    public void setNo(String VehicleNo)
    {
    this.VehicleNo=VehicleNo;
    }

    public String getNo()
    {
    return VehicleNo;
    }


    public void setModel(String Model)
    {
    this.Model=Model;
    }

    public String getModel()
    {
    return Model;
    }

    public void setManufacturer(String Manufacturer)
    {
    this.Manufacturer=Manufacturer;
    }

    public String getManufacturer()
    {
    return Manufacturer;
    }



    public void setColor(String Color)

    {
    this.Color=Color;
    }

    public String getColor(String s)
    {
    return s;
    }

}


class Truck extends Vehicle

{

    double LoadingCapacity;

    public void setLoad(double LoadingCapacity)
    {
    this.LoadingCapacity=LoadingCapacity;
    }


    public double getLoad(double ld)
    {
    return ld;
    }
}

public class VehicleInfo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void mainEntry2() throws IOException
    {   
        //Truck D=new Truck();
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Color: ");
        String col=br.readLine();
        D.setColor(col);

        System.out.print("Enter Loading Capacity: Maximum 100tons");
        int load=Integer.parseInt(br.readLine());
        D.setLoad(load);
    }
    public static void main(String[] args) throws IOException 
        {
            int loop_option=0;
            public Truck D=new Truck();
            public BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Vehicle No: ");
            String no=br.readLine();
            D.setNo(no);

            System.out.print("Model: ");
            String model=br.readLine();
            D.setModel(model);

            System.out.print("Manufacturer: ");
            String man=br.readLine();
            D.setManufacturer(man);

mainEntry2();
            do
            {
                System.out.println("");

                System.out.println("----Vehicle Information----");
                System.out.println();
                System.out.println("Vehicle No: "+D.getNo());
                System.out.println("Model: "+D.getModel());
                System.out.println("Manufacturer: "+D.getManufacturer());
                System.out.println("Color: "+D.getColor(col));
                System.out.println("Loading Capacity: "+D.getLoad(load));
                System.out.println("");
                System.out.println(" if U want to update color and loading capacity press 1 and to exit press 2 ..");
                loop_option=Integer.parseInt(br.readLine());
                if(loop_option==1)
                {
                    mainEntry2();
                }

            }while(loop_option==1);

        }

    }

您应该从这里开始学习Java: http : //download.oracle.com/javase/tutorial/

Here are some general remarks. Hope they help:

  1. getters should only return value, they do not have parameters (otherwise they are not getters).
  2. if you're encapsulating fields, make them private
  3. use descriptive variable names (eg. truck instead of D)
  4. use descriptive method names ( mainEntry2() tells absolutely nothing)
  5. if you're parsing integer from user input, handle parsing exceptions (like NumberFormatException )

For instance, your program may be re-written like here:

import java.io.*;

class Vehicle {
    private String vehicleNo;
    private String model;
    private String manufacturer;
    private String color;

    public void setNo(String vehicleNo) {
        this.vehicleNo = vehicleNo;
    }

    public String getNo() {
        return vehicleNo;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

}

class Truck extends Vehicle
{
    private double loadingCapacity;

    public void setLoad(double loadingCapacity) {
        this.loadingCapacity = loadingCapacity;
    }

    public double getLoad() {
        return this.loadingCapacity;
    }

}

public class VehicleInfo {

    private static void updateColorAndCapacity(BufferedReader br, Truck truck)
        throws IOException
    {
        System.out.print("Color: ");
        String col = br.readLine();
        truck.setColor(col);

        while (true)
        {
            System.out.print("Enter Loading Capacity (maximum 100tons):");
            String value = br.readLine();
            value = value.trim();
            try {
                int load = Integer.parseInt(value);
                truck.setLoad(load);
                return;
            } catch (NumberFormatException e) {
                /// do it once again
                continue;
            }
        }
    }

    public static void main(String[] args) 
        throws IOException {

        Truck truck = new Truck();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Vehicle No: ");
        String no = br.readLine();
        truck.setNo(no);

        System.out.print("Model: ");
        String model = br.readLine();
        truck.setModel(model);

        System.out.print("Manufacturer: ");
        String man = br.readLine();
        truck.setManufacturer(man);

        updateColorAndCapacity(br, truck);

        int loop_option = 0;        
        do {
            System.out.println();
            System.out.println("----Vehicle Information----");
            System.out.println();
            System.out.println("Vehicle No: " + truck.getNo());
            System.out.println("Model: " + truck.getModel());
            System.out.println("Manufacturer: " + truck.getManufacturer());
            System.out.println("Color: " + truck.getColor());
            System.out.println("Loading Capacity: " + truck.getLoad());
            System.out.println();
            System.out.println(" if U want to update color and loading capacity press 1 and to exit press 2 ..");

            loop_option = Integer.parseInt(br.readLine());

            if (loop_option == 1) {
                updateColorAndCapacity(br, truck);
            }

        } while (loop_option == 1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM