Reputation: 1499
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);
}
}
Upvotes: 2
Views: 470
Reputation: 7662
Here are some general remarks. Hope they help:
mainEntry2()
tells absolutely nothing)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);
}
}
Upvotes: 2
Reputation: 54790
You should start here for learning Java: http://download.oracle.com/javase/tutorial/
Upvotes: 2