• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help in coding the below assignment, please give me hint  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Display Shipment Details       
Best Flight is an emerging logistics Company in the City and is evolving the way goods are moved with the power of technology. There are plenty number of Dealers and Customers using Best Flight for shipping their goods. Therefore it essential to connect their Dealers and Customers to Shipment Management System to know the entire details and status of their Shipments. Write a piece of code strictly following the below specifications to get the Shipment details and print the output. 

  This problem will help you understand about Static Variable and Static Method.

Create a class named Shipment with the following private member variables / attributes


    int id
    String shipperName
    String modeOfTransportation
    Float totalWeight
    String arrivalPort
    String departurePort
1. Create a private static variable nextShipmentId in Shipment class and initialise it to 1000.



Hint : static The static variable gets memory only once in class area at the time of class loading. We can apply static keyword with variables, methods, blocks and nested class. The static variable can be used to refer the common property of all objects. It makes your program memory efficient. Eg : static nextShipmentId = 1000;
2. Create a Constructor which accepts 6 arguments in the argument order (id,shipperName,modeOfTransportation,totalWeight,arrivalPort,departurePort)


Hint : Constructor A constructor in Java is a block of code similar to a method that's called when an instance of an object is created. A constructor doesn't have a return type. Eg : public Shipment(id,shipperName,modeOfTransportation,totalWeight,arrivalPort,departurePort)
3. Create a static method createNewShipment() - which accepts the shipment details in the parameter and generates the shipment object and assigns the shipment id and return the shipment object. For returning the object pass the values in the constructor and return it.


public static Shipment createNewShipment(String shipperName,String modeOfTransportation,Float totalWeight,String arrivalPort,String departurePort)
The id assigned to each shipment must be unique and it must be in order. The first shipment should have the id as 1001, the next shipment should have the id as 1002 and so on.


Hint : return Type Declare a method's return type in its method declaration. Within the body of the method, use the return statement to return the value. Any method declared void doesn't return a value. Eg : public static Shipment createNewShipment(){  return returnValue; }
4. Display the shipment details by overriding the toString() method in Shipment class.
Use String.format("%-15d%-15s%-25s%-15s%-20s%s",this.id,this.shipperName,this.modeOfTransportation,this.totalWeight.toString()+"Kg",this.arrivalPort,this.departurePort)  to format the string.

Hint : toString() The toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String  representation of the Object. Eg : public String toString() {  // return the formatted String. - Override the toString() method here }
Create a Main Class and write a main method in it and test the above class.

Problem Specification :
The toString() method will overrides the object representation into required format. If we try to print the object, the toString() method will be automatically invoked and the changed representation of the string will be printed as shown in Sample input and Output.
Print the "Shipment details are" String and the table header details in main method itself. The toString() method will only contain the format for printing the object.

[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, attribute names and method names.]



Input and Output Format:
Refer sample input and output for formatting specifications.

Sample Input and Output :
[All text in bold corresponds to the input and the rest corresponds to output.]
Enter the number of shipments :
3
Enter the shipment 1 details
Enter the shipper name :
AGCI
Enter the mode of transportation :
Air
Enter the total weight :
300
Enter the arrival port :
India
Enter the departure port :
America
Enter the shipment 2 details
Enter the shipper name :
DHL Services
Enter the mode of transportation :
Water
Enter the total weight :
500
Enter the arrival port :
Cochin
Enter the departure port :
Srilanka
Enter the shipment 3 details
Enter the shipper name :
Fedex
Enter the mode of transportation :
Land
Enter the total weight :
200
Enter the arrival port :
India
Enter the departure port :
China
Shipment details are
Id                 Shippername            Mode of transportation   Total weight   Arrival port        Departure port          
1001           AGCI                        Air                                 300Kg                India                      America           
1002           DHL Services            Water                             500Kg               Cochin                  Srilanka           
1003           Fedex                       Land                              200Kg                India                      China
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to think in this exercise at all. What you need to do is just follow what is said to type. Probably you need to know syntax only.
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what have you came up with so far and tell us where you stuck.

And welcome to the Ranch.
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have created so far. In the problem statement, I didn't understand how to create a static method to return the object. so I have commented the code for now. I need help in this area.

Shipment.java

public class Shipment {
    private int id;
    private String shipperName;
    private String modeOfTransportation;
    private Float totalWeight;
    private String arrivalPort;
    private String departurePort;
   
    private static int nextShipmentId=1000;
   
    public Shipment(int id,String shipperName,String modeOfTransportation,Float totalWeight,String arrivalPort,String departurePort)
    {
        this.id = id;
        this.shipperName = shipperName;
        this.modeOfTransportation = modeOfTransportation;
        this.totalWeight = totalWeight;
        this.arrivalPort = arrivalPort;
        this.departurePort = departurePort;
    }
    /*public static Shipment createNewShipment(String shipperName,String modeOfTransportation,Float totalWeight,String arrivalPort,String departurePort)
    {
        this.shipperName = shipperName;
        this.modeOfTransportation = modeOfTransportation;
        this.totalWeight = totalWeight;
        this.arrivalPort = arrivalPort;
        this.departurePort = departurePort;
        return s;
    }*/   
    public String toString()
    {
        return String.format("%-15d%-15s%-25s%-15s%-20s%s",this.id,this.shipperName,this.modeOfTransportation,this.totalWeight.toString()+"Kg",this.arrivalPort,this.departurePort);
    }
}

Main.java

import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])throws IOException{
    int noOfShipments;
    int i;
    Integer[] shipmentId;
    String[] shipperName;
    String[] modeOfTransport;
    Float[] totalWeight;
    String[] arrivalPort;
    String[] departurePort;
   
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of shipments :");
    noOfShipments = sc.nextInt();
    shipmentId = new Integer[noOfShipments];
    //shipmentId[0]=1000;
    shipperName = new String[noOfShipments];
    modeOfTransport = new String[noOfShipments];
    totalWeight = new Float[noOfShipments];
    arrivalPort = new String[noOfShipments];
    departurePort = new String[noOfShipments];
   
    for (i=0;i<noOfShipments;i++)
    {
       System.out.println("Enter the shipment "+(i+1)+" details");
      
       shipmentId[i]=1001+i;
      
       System.out.println("Enter the shipper name :");
       shipperName[i]=sc.next();
      
       System.out.println("Enter the mode of transportation :");
       modeOfTransport[i] = sc.next();
      
       System.out.println("Enter the total weight :");
       totalWeight[i] = sc.nextFloat();
      
       System.out.println("Enter the arrival port :");
       arrivalPort[i] = sc.next();
      
       System.out.println("Enter the departure port :");
       departurePort[i] = sc.next();
    }
   
    Shipment[] s = new Shipment[noOfShipments];
    System.out.println("Shipment details are");
    System.out.println("Id                 Shippername            Mode of transportation   Total weight   Arrival port        Departure port");
        for (i=0;i<noOfShipments;i++)
        {
            s[i] = new Shipment(shipmentId[i],shipperName[i],modeOfTransport[i],totalWeight[i],arrivalPort[i],departurePort[i]);
            System.out.println(s[i]);
        }
   
}
}
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Saloon Keeper
Posts: 3332
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You realize the "this." as it is used here is redundant.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
You realize the "this." as it is used here is redundant.

I always use this. to prefix member variables regardless of whether it is redundant or not.
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when you ran program with sample inputs, did you get specified expected output?
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I am getting the desired output with the only below exception:

The requirement is, if the user enter the float value input as 400.5 then the system should print it as 400.5, but if the user enter the input as 400 then it should NOT print 400.0, rather it should only print 400. I don't know how to do this? Please help.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just an example and not a complete description of the requirements, right? However what I would suggest is this: if the number is an integer then use the formatting code for an integer, otherwise use the formatting code for a double value with one decimal place. I'm referring to the formatting codes used by the String.format method, of course.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!