Question

I need to write a Java Programm based on the follwoing facts: A company that operates...

I need to write a Java Programm based on the follwoing facts:

A company that operates regional trains wants to model the utilization of their trains. For this she has created the following UML class diagram.

Train

-capacity:int

-numberPassengers:int

+Train(capacity:int)

+getcapacity():int

+getNumberPassengers();int

+setNumberPassengers(numbersPassengers:int):void

The exercise i need help with is as follows:

Write a Java class train for the given class diagram and implement all the methods. The number of passengers of new instances should be 0. The method setNumberPassengers should set the attribute numberPassengers

• to the value 0, if the value of the parameter passenger-number is less than 0,

• to the value of the attribute capacitance, if the value of the parameter passenger-parameter is greater than this value,

• and otherwise to the Value of parameter numberPassage

b) Write a Java class train simulator using a main method that 1

• creates a train with capacity 100,

• simulates the change in the number of passengers for stops 1, ..., 40, whereby

• when arriving at stop number i, a random number of passengers first disembark (equally distributed, maximum 40 persons),

• after that, a random number of passengers descend (equally distributed, maximum 40),

• the number of passengers, when the train leaves the i-th stop, on the console outputs.

If more people want to get off than on the train, or more people board than the train allows because of its capacity, the setter for countless passengers should take care of it. The random number generation does not need to be adjusted. Notes: For random numbers, you should use the Math.random method

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the code below for the problem statement. I have added the snapshot of the output below.

import java.lang.Math;
class Train{
  
private int capacity;
private int numberPassengers;
  
public Train(int capacity){
this.capacity = capacity;
setNumberPassengers(0);
}
  
public int getcapacity(){
return capacity;
}
  
public int getNumberPassengers(){
return numberPassengers;
}
  
public void setNumberPassengers(int numbersPassengers){
if(numbersPassengers<0){
this.numberPassengers = 0;
}
else if(numbersPassengers>capacity){
this.numberPassengers = capacity;
}
else{
this.numberPassengers = numbersPassengers;
}
}
}

public class TrainSimulator{
  
public static void main(String []args){
Train train = new Train(100);
int MAX_PPL = 40;
int tmp = 0;
for(int stop = 1; stop <= 40 ; stop++){
// passengers getting out
tmp = train.getNumberPassengers() - (int)(Math.random() * (MAX_PPL + 1));
train.setNumberPassengers(tmp);
// passengers getting in
tmp = train.getNumberPassengers() + (int)(Math.random() * (MAX_PPL + 1));
train.setNumberPassengers(tmp);
System.out.println("Number of passenger in Train while leaving station "+stop+" is "+train.getNumberPassengers());
}
  
  
}
  
}

OUTPUT:

Number of passenger in Train while leaving station 1 is 18 Number of passenger in Train while leaving station 2 is 24 Number

I hope it helps.

Please revert back to me in case of any query and give a thumbs up if you like the solution.

Have a great day :)

Add a comment
Know the answer?
Add Answer to:
I need to write a Java Programm based on the follwoing facts: A company that operates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help asap, Python language 2) Write a class to represent a class to represent...

    I need help asap, Python language 2) Write a class to represent a class to represent a Car and its passengers. Write a constructor that takes one additional parameter a number, the total seats in the car. The constructor should save this as a datamember and also initialize an additional datamember, a list, to keep track of the passengers in the car. Write method addRider that takes an additional parameter a string of the passenger to be added to the...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one as...

    java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one assignment per team of two via Omnivox and NOT MIO.Assignments sent via MIO will be deducted marks. Assignments must be done alone or in groups and collaboration between individuals or groups is strictly forbidden. There will be a in class demo on June 1 make sure you are prepared, a doodle will be created to pick your timeslot. If you submit late, there...

  • Write a java programm that calculates the total of a retail sale should ask the user...

    Write a java programm that calculates the total of a retail sale should ask the user for the following: The retail price of the item being purchased The sales tax rate Once these items have been entered, the program should calculate and display the following: The sales tax for the purchase The total of the sale I tried doing it here. but it is not giving me the right answer. kindly help correct the errors. import java.util.Scanner; public class SalesTax...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • C++ ONLY Please TRAINS DESCRIPTION You are completing a program that allows for several different types...

    C++ ONLY Please TRAINS DESCRIPTION You are completing a program that allows for several different types of passenger trains. One train can hold only cats. Another train can hold only wizards. You are going to create a Wizard class, a Cat class, and a Train class. You are provided the driver, lab2.cpp. The Train class should be a template class where the type of passenger is the template data type. Specifications cat class Attributes: name of cat breed of cat...

  • (JAVA) 1) Write a blueprint class that tracks the orders made to vendors in a company's...

    (JAVA) 1) Write a blueprint class that tracks the orders made to vendors in a company's product purchasing system. We will create a class called ProductOrder. The requirements are as follows: a) The class ProductOrder has the following private instance variables:productID orderld, quantityRequested quantity Fulfilled all of types integer. We will need separate variables for quantity requested and quantity fulfilled because the vendors may not be able to fulfill the requests completely b) The class/object model will provide the values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT