Good afternoon, I need a DML diagram for the following exercises. 1. Write Java
ID: 3736862 • Letter: G
Question
Good afternoon,
I need a DML diagram for the following exercises.
1. Write Java code to define the class named Information that contains:
- An int data field representing the identification number for an item
- A String data field representing the name of an item
- A no-arg constructor to initialize the field members, so identification number is 1000 and the name = "desk"
- A constructor to create an object with a given identification number and a name
- A copy constructor that accepts an object of the class Information and sets the data values, accordingly.
- Appropriate Setter and Getter methods
- A toString() method to allow for displaying the values of data fields. This method should return the values of the data fields.
2. Write Java code to define a class named SomeClass that contains:
- Two double data fields that specify the center of a circle (cx, cy), one data field that specifies the radius (radius) of a circle, and one double data field that specifies the diameter (diameter) of a circle. Also, include a data field that represents an Information object. You defined the Information class in question 1.
- A no-arg constructor that creates a circle with center (2, 2), and radius of 5. Also, set the values for the Information object from the class created in question 1.
- A constructor that creates a circle with the given values for the values of cx, cy, radius, name and identification number using the Information class created in question 1.
- A method to return the area of a circle. Area of a circle is * radius2 A method to return the diameter of a circle. Diameter of a circle is radius * 2
- A method to return the perimeter of a circle. Perimeter of a circle is 2 * * radius
- A toString() method to display all of the attributes of the class Define a test class that includes the main method to define at least 5 objects showing the use of constructors and methods of the class.
Explanation / Answer
Code:
public class TestInformation {
public static void main(String[] args) {
// TODO Auto-generated method stub
Information[] infoObjects = new Information[3];
Scanner sc = new Scanner(System.in);
int i=0;
int id;
String name;
while(i<3){
System.out.println("Enter item id: ");
id = sc.nextInt();
System.out.println("Enter item name: ");
name = sc.next();
infoObjects[i] = new Information(id, name);
i++;
}
for(i=0;i<infoObjects.length;i++){
System.out.println(infoObjects[i].toString());
}
Information copyObject = new Information(infoObjects[2]);
System.out.println(copyObject.toString());
}
}
class Information{
int itemID;
String name;
Information(){
this.itemID = 1000;
this.name = "desk";
}
Information(int id, String n){
this.itemID = id;
this.name = n;
}
Information(Information info){
this.itemID = info.getItemID();
this.name = info.getName();
}
public int getItemID() {
return itemID;
}
public void setItemID(int itemID) {
this.itemID = itemID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return ("ItemId: "+this.getItemID()+" Name: "+this.getName());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.