trying to create an object from different classes with composition relationship
i'm trying to do my university project but the problem with my code is such: I made a class called Booking it has attributes and a constructor,Booking has a composition relationship with class Client. next class called Company and it has a composition relationship with Booking, as well another abstract class called vehicle, inside company I created a method and in it a bunch of operations, then I tried to create an object from class Booking but errors keeps showing. syntax is right i think??
Booking bookings = new Booking(int period,int personCapicity,Client client, Vehical vehical, int days, String Status, int id);
this object is inside an if statement
heres what is in the error note
'class' expected
';' expected
not a statement
do you know?
how many words do you know
See also questions close to this topic
-
Read each name in Array list to create seperate object for
I have a file that has student names, age, and an id number. I have a student class that holds the everything above for each student object. I stored all the names, id numbers. and age separately in an array list. Now im trying to assign the info to create a student object.
public class Student { private String lName; private int idNumber; private int age; public Student() { lName = ""; idNumber = 0; age = 0; } public Student(String l, int i, int a) { lName = l; idNumber = i; age = a; } public void setName(String last) { lName = last; } public String getName() { return lName; } public void setIdNum(int num) { idNumber = num; } public int getIdNum() { return idNumber; } public void setAge(int a) { age = a; } public int getAge() { return age; } }
My Text File looks something like this: This info is stored in parallel array lists. I don't quite get how to implement this into an object to pass it into my second contractor method.
Josh 2134 19 Smith 5256 21 Rogers 9248 19 Andrew 7742 20
Here's what I've tried;
public static void main(String[] args) { String file = "studentData.txt"; Scanner reader = new Scanner(file); ArrayList<String> lastNames = lNames(file); ArrayList<Integer> idNumbers = idNum(file); ArrayList<Integer> ageList = ages(file); Scanner input = new Scanner(System.in); Student s1 = new Student(); // confused about how to implement this constructor with the textile info for (int i = 0; i<idNumbers.size(); i++) { Student user = new Student(lastNames.get(i), idNumbers.get(i), ageList.get(i)); } //user enters idNumber to display age System.out.println("Enter ID Number"); //exception handling to be added int idNum = input.nextInt(); for (int i = 0; i<idNumbers.size(); i++) { if (idNum == idNumbers.get(i)) { s1.setAge(ageList.get(i)); System.out.println(s1.getAge()); } } }
-
Using EdittextPreference for Goto search
sorry for my poor English. I want to use EditTextPreference in my bottom nav like the pic below, ![screenshot][1]
I have recycleview xml in my project with in many sub cardview layouts(which is scrollable) and I want to create item in the bottom nav which is called "Goto". When the "Goto" item is clicked i want it to pop-up like the screenshot. And when user enters a number(according to the cardviews i.e if the number of cardview is 40 user must enter 1-40) I want to search the cardview by its ID. Thank you and I hope u got it, If u have any questions let me know [1]: https://i.stack.imgur.com/grK8P.jpg
My xml format look like this. As you see in the blow since the cardviews are huge in number it is not cool to scroll all the way down that is why i need Goto item in the bottom nav to search it by its ID when the user click number in the EditTextPreference as u see in the screenshot. i.e The screenshot is not from my app
<LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout> <androidx.cardview.widget.CardView> <RealtiveLayout> <Textview/> <RealtiveLayout> </androidx.cardview.widget.CardView> </LinearLayout> .. .. .. .. many more..
-
How to get remaining time of the day in java?
I would like to calculate the time remaining for next day 00:00:00 from the current date time.
For e.g. time difference between 2
022-05-07T05:49:41.883807900Z
and2022-05-08T00:00:00Z
Expected answer:
18:10:19
or 65419 (in seconds).How can I achieve this with efficiently using java 8?
-
How to convert String having key=value pairs to Json
myString =
{AcquirerName=abc, AcquiringBankCode=0.2, ApprovalCode=00};
I want to convert it to the following string.
{"AcquirerName": "abc", "AcquiringBankCode": 0.2, "ApprovalCode": 0};
How can I do it in java?
-
VBA: Creating a class property that is an array of dictionaries
In Microsoft Excel VBA I need to create a class that has two properties, "Name" and "Holdings". "Name" is just a string so the code for that is easy. But I need "Holdings" to be a variable length array containing dictionary objects. So this is what I wrote:
Private mName As String Private mHoldings() As Scripting.Dictionary Public Property Let Name(vName As String) mName = vName End Property Public Property Get Name() As String Name = mName End Property Public Property Set Holdings(vHoldings() As Scripting.Dictionary) Set mHoldings = vHoldings End Property Public Property Get Holdings() As Scripting.Dictionary Set Holdings = mHoldings End Property
When I try to compile it gives me this error: "Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalide Set final parameter.
What I doing wrong?
-
How to access an object inside another object in a map in react
react.js is complicated sometimes, I'm trying to access an information of a state, I have an array which has one object inside, and in this object, there is another object called price, and in this last object there is one property called price too, and when I try to get this information in a map function, the code breaks, this is my map code: (the error line is in ******) the error show like this: Uncaught TypeError: Cannot read properties of undefined (reading 'price')
products.map((item) => { return ( <MainContainer onMouseEnter={() => onEnter(item.id)} key={item.id}> <Card> <TopContainer> <p>163892</p> <h2>{item.name}</h2> <Icons> <svg clip-rule="evenodd" fill-rule=</svg> <InfoOutlinedIcon/> </Icons> </TopContainer> <hr/> <MidContainer> <img src='https://cfarma-public.s3-sa-east-1.amazonaws.com/images/nexfar-product-default-image.jpg'/> <div> <p>Base</p> ****************************************<p>Nexfar<br/>R${item.price.price}</p>******************** </div> <div></div> <div></div> <div></div> <div></div> </MidContainer> </Card> </MainContainer> ); })
this image shows how the objects structure is
Thank you guys!
-
How do I make my setfunction run through my if loop?
I am working on a program and I need to make a class about cars. I need to add setFunctions that only allow a car year manufacture of 1930 -2030 as input, anything else will be automatically set as 2012. My code looks like this:
// code will be following void setYear(int yr) { if (yr > 2030 || yr < 1930){ yr = 2012; } else { year = yr; } } // end of segment.
I also have code for setSpeed which cannot be less than 0 as follows
// code following void setSpeed(int spd) { if (spd < 0){ spd = 0; } else { speed = spd; } }
The problem is, when I try change my variables for my objects, the set functions don't work as desired. I believe the problem is with my constructor,
// constructor with default parameters car(int yr = 2012, string mk = "unknown", int spd = 0) { year = yr; make = mk; speed = 0; }
My code basically does the project as required, it's just not exactly what is asked.
This is what my code is supposed to do:
Create a program that illustrates C++ classes. Your program should do the following:
Write a class named Car that has the following member variables:
year (An int that holds the car’s model year. The year must be between 1900-2030 (inclusive)).
make (A string that holds the make of the car.)
speed (An int that holds the car’s current speed. The speed must be 0 or greater. ) In addition, the class should have the following member functions:
Constructor - The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables. The constructor should initialize the speed member variable to 0. Notice that this constructor will have 2 parameters: one for the year and one for the make of the Car.
When you define a Car object, you will need to send the two arguments the constructor is expecting:
Car myCar(2002, "Jetta");
In the constructor you need to assign a value to each of the member variables:
- Since year has specific values that are allowed, call the setYear function with the value sent.
- Set make to the value passed.
- Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.
Get functions:
Appropriate access functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables. There will be three get functions, one for each member variable. Each of these functions return one thing and have NO parameters.Set functions:
There will be three set functions, one for each member variable. Each of these functions receive one thing and return nothing.- The speed cannot be less than zero. Have your set function protect the speed so it is never negative. If a negative value is sent, simply set the speed to zero.
- The year must be between 1900 and 2030 (inclusive). If a year outside the range is sent, set the year to 2012. No validation of the make is required.
Accelerate:
The accelerate function should add 5 to the speed member variable each time it is called. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it adds 5 to speed.Brake:
The brake function should subtract 5 from the speed member variable each time it is called. If the speed is already zero, no change is to be made to speed. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it subtracts 5 from speed unless speed is already at zero. Then it makes no change.Notes:
Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it.Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.
Now, set the year, make, and speed to 2009, Jeep, and 75 respectively. Once again call the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.
Finally, set the year, make, and speed to 1827, Jetta, and -30 respectively. Using the get functions, print out the year, make, and speed.
This is my code:
#include <iostream> #include <cstdlib> #include <string> using namespace std; // car class declaration class car { private: int year; string make; int speed; public: //creation of set functions void setYear(int yr) { if (yr > 2030 || yr < 1930){ yr = 2012; } else { year = yr; } } void setMake(string mk) { make = mk; } void setSpeed(int spd) { if (spd < 0){ spd = 0; } else { speed = spd; } } // constructor with default parameters car(int yr = 2012, string mk = "unknown", int spd = 0) { year = yr; make = mk; speed = 0; } // Accessors ( The get functions) int getYear() { return year; } string getMake() { return make; } int getSpeed() { return speed; } // Mutators void accelerate () { speed += 5; } void brake() { if ( speed >= 5) speed -=5; else speed = 0; } }; int main() { // creation and display of first object of car class car hotRod (2020, "Ford"); hotRod.setSpeed(40); cout << "I've spotted a " << hotRod.getYear()<<" " << hotRod.getMake() << " car, going 40 kmph!\n\n"; // accelerator function to run 5 times cout << "It is accelerating \n\n"; for (int run =0; run < 5; run++) { hotRod.accelerate(); cout << hotRod.getYear()<<" " << hotRod.getMake() << " current speed: " << hotRod.getSpeed() << "kmph.\n"; } cout << endl; // breaking function to run 7 times cout << "Now it's 'breaking... " << endl; for(int brk = 0; brk < 7; brk++) { hotRod.brake(); cout << hotRod.getYear()<<" "<< hotRod.getMake() << " Current speed: " << hotRod.getSpeed() << "kmph.\n"; } // intermediary input to break the program apart // this allows for better display of program string x; cout << endl << endl << endl << "Would you like to know what car I saw next? "; cin >> x; // creation and display of second object in car class car hotRod2 (2009, "Jeep"); hotRod2.setSpeed(75); cout << endl << endl <<endl << "I've spotted a " << hotRod2.getYear()<<" " << hotRod2.getMake() << " car, going 75kmph!\n\n"; // accelerator function to run 5 times cout << "It is accelerating \n\n"; for (int run =0; run < 5; run++) { hotRod2.accelerate(); cout << hotRod2.getYear()<<" " << hotRod2.getMake() << " current speed: " << hotRod2.getSpeed() << "kmph.\n"; } cout << endl; // Breaking function to run 7 times cout << "Now it's 'breaking... " << endl; for(int brk = 0; brk < 7; brk++) { hotRod2.brake(); cout << hotRod2.getYear()<<" "<< hotRod2.getMake() << " Current speed: " << hotRod2.getSpeed() << "kmph.\n"; } string y; cout << endl << endl << endl << "Would you like to know what car I saw next? "; cin >> y; // creation of 3rd object of car class car hotRod3; hotRod3.setMake("Jetta"); hotRod3.setYear(1827); hotRod3.setSpeed(-30); cout << endl << endl <<endl << "I've spotted a " <<hotRod3.getYear() <<" " << hotRod3.getMake() << " car, going " << hotRod3.getSpeed() << "kmph! \n\n"; // end of program return 0; }
-
Constructor __init__ is written to take two position arguments, but when used, reports the error that only 1 is allowed
I have a class and constructor I'm working on, and I added an extra parameter to
__init__
, and now I get the error,TypeError: FeatureDataset() takes 1 positional argument but 2 were given
.I wonder why. It seems to me that it should accept two arguments. It's an incomplete function, but I'd like to get past this constructor argument number error. I have checked several answers and either they were about something specifically different, or indentation, and I have neither of those issues (4 indents per new indentation divide).
def FeatureDataset(Dataset): def __init__(self, root_dir, file_name): #load csv self.file_out = pd.read_csv(file_name) self.root_dir = root_dir self.labels = self.file_out.iloc[1:160, 0].values self.features = self.file_out.iloc[1:160, 1:].values #Feature Scaling sc = StandardScaler() label_train = self.labels feature_train = self.features #sc.fit_transform(features) #Convert to torch tensors self.feature_train = torch.tensor(label_train, dtype = torch.float32) self.label_train = torch.tensor(label_train) file_name = "data.csv" root_dir = "archive" feature_set = FeatureDataset(root_dir, file_name)
-
Filter full path in Hierarchy chart Power BI
I am creating a family tree using a Hierarchy Chart by Akvelon. I have added one table under the chart to present the people's data.
When I select any node it will filter the selected person only. enter image description here
but I want to filter the full path of nods under this person. enter image description here
any idia how to make it?
Raw Data: enter image description here
-
T-SQL creating a hierarchy out of orderly numbers
I have such table:
Id code 1 10 2 11 3 20 4 21 5 30 6 31 7 32 8 40 9 10 10 11 11 20 12 21 13 30 14 31 15 32 16 40 17 20 18 21 19 30 20 31 21 32 22 40 23 20 24 21 25 30 26 31 27 32 28 40 29 20 30 21 31 30 32 31 33 32 34 40 35 20 36 21 37 30 38 31 39 32 40 40 41 41 42 90
The column
id
represents simply the order of the records.
The columncode
represent the type of record.The problem is that the records are part of a hierarchy, as shown here:
What I need to obtain is the parent of every record:
Id code Parent 1 10 1 2 11 1 3 20 1 4 21 3 5 30 3 6 31 3 7 32 3 8 40 3 9 10 9 10 11 9 11 20 9 12 21 11 13 30 11 14 31 11 15 32 11 16 40 11 17 20 9 18 21 17 19 30 17 20 31 17 21 32 17 22 40 17 23 20 9 24 21 23 25 30 23 26 31 23 27 32 23 28 40 23 29 20 9 30 21 29 31 30 29 32 31 29 33 32 29 34 40 29 35 20 9 36 21 35 37 30 35 38 31 35 39 32 35 40 40 35 41 41 40 42 90 42
The parent of every record should be expressed as its
Id
.The rules are like this:
- 10s are their own parents since they are the roots
- 90s are their own parents since they are the end of data
- 20s parent is the previous 10
- 21 30 31 32 33 parent is the previous 20
- 40 and 50 parents is the previous 20
- 41 parent is the previous 40
As you can see the order in which records are is very important.
I tried to solve this declaratively (with
lag()
etc) and imperatively with loops but I could not find a solution.Please help
-
Can you paste custom images & data to PC with an electric piano?
I play the piano by ear and am developing a composition method (alternative to traditional sheet music) to compliment this style. The problem is I haven't been able to find a way to record songs live, as I play them on my electronic piano, into a PC program (Word, web app, etc.)
In theory, it seems simple enough:
a) Play a note on my electronic piano (USB 2.0 Type A to PC connection) b) PC recognizes the note played and pastes a pre-defined image/character into a program to represent the note c) Play the next note, rinse & repeat
I've found programs that do this in traditional composition for standard sheet music, but I haven't found one that would allow me to modify the input images/characters.
I've recently started learning Python coding through Udemy. Curious to know if anybody knows how difficult it would be to create or modify a program that would do this?
Thank you.
-
how to change a child class values inside its parent class
is there a way I could access child class methods when it's made within a parent class? for example:
public class company { public static void main(String[] args){ Employee obj1 = new Employee("sara",9000,new Address(4461,11234,"first street")); }
what I'm trying to do is change the Address from "first street" to "second street" using a setter method. but I'm not sure how since its initialized inside of class Employee.
-
Is it okay to use inheritance just to reuse common attributes and isolate validation?
I've got two java classes in a Spring Boot project which are quite similar. They have several attributes in common (id, name, ... at least 10 attributes), but they are different because ClassA has a list enum values and ClassB has just a single enum value and ClassZ instance.
I've thought about making an abstract superclass with all attributes, so ClassA just extends it including the list attribute and ClassB extends it including the enum and the ClassZ instance.
Another way would be that ClassB extends ClassA and in the lists just appears the enum value needed by ClassB and include the ClassZ instance.
A third way would be to merge ClassA and ClassB in the same class, and have no inheritance relationship.
The main doubt here is that on the one hand I think the first option would allow me to segregate the specifications and validation for ClassA and ClassB, but I'm not using the advantages from inheritance. The third option doesn't include inheritance that might increase difficulty, but it merges all validations with many if-else checks.
For example:
- if the enum value list contains 'typeZ', then the ClassZ instance should be not null.
- if the enum value list contains 'type1' and 'type3' then ClassZ instance should be null
- 'typeZ' souldn't appear with more enum values, and it necessary has to appear with ClassZ instance. Other enum values can appear together, but ClassZ instance should be null.
For this logic, what option should I choose? I'm using Spring validations and custom validations to validate the entities.