java skeleton code from design class diagram. Remove error - java

I was trying to write Java code for this design class Diagram. This question came in past exams.
I couldn't properly understand the relation of FullTimeStudent and PartTimeStudent with PHDprogram. If there is any other mistake in my code do correct me.
Here is my code
class student{
int registration;
String name;
public void setName ( String name ) {
this.name = name;
}
}
class FullTimeStudent extends student{}
class PartTimeStudent extends student{}
interface course{
void setInstructorName(String name);
}
class PhdCourse implements course{
int courseCode;
String coursename;
String courseArea;
String courseInstructor;
#Override
public void setInstructorName (String name) { this.courseInstructor = name;
}
}
class PHDTheasis{ int code; String topic; String area; }
class PHDprogram{
FullTimeStudent ft;
PartTimeStudent pt;
int deptcode;
public void registerCourse(PhdCourse c){}
public void registerTheasis(PHDTheasis t){}
}

Here is image from Wikipedia and other answer from stackoverflow Direction of the association arrow in UML class diagrams
Summarizing this two sources your code should look next way
class PHDprogram {
List<FullTimeStudent> ft;
PartTimeStudent pt;
...
}
And optionally you can add relation from FullTimeStudent and PartTimeStudent sides
class PartTimeStudent {
PHDprogram phdProgram;
}
class FullTimeStudent {
PHDprogram phdProgram;
}

Related

Giving the object to create as parameta in constuctor

sorry for the title I don't know how to say that.
So basically what I want to do is the following:
public class TestClass {
private final String name;
public TestClass(String name) {
this.name = name;
}
public TestClass(TestClass test) {
this = test;
}
public String getName() {
return name;
}
}
So create a class where you can give an object from itself in the constructor
I don't know if that is possible at all.
The problem I have is that I have multiple objects that extend from this class and I want a simple way to pass them to the next class
so when I have 2 classes
TestClass2 extends TestClass
TestClass3 extends TestClass
and I want to create an instance of the testclass3 from the testclass2 one.
they both should have the same name from testcalss
currently, I am doing that like that:
private class TestClass2 extends TestClass{
private final String anotherName;
public TestClass2(String name, String anotherName) {
super(name);
this.anotherName= anotherName;
}
private void createTest3(String whatever) {
new TestClass3(this, whatever);
}
}
private class TestClass3 extends TestClass{
private final String whatever;
public TestClass3(TestClass test, String whatever) {
super(test.getName());
this.whatever = whatever;
}
}
In my case, my base class has not just a name but a lot more values that I then have to submit!
I hope you kinda understand what I want to say. Again sorry I explained that very bad :D
And thank you all thanks in advance for any answers!

How can I express object which should be 2 different classes at the same time

Today I had test in OOP and I was given the following task to code:
Imagine you have two classes: Employee (which represents being an employee) and Ninja (which represents being a Ninja). An Employee has both state and behaviour; a Ninja has only behavior. You need to represent an employee who is also a ninja (a common problem in the real world). By creating only one interface and only one class (NinjaEmployee), show how you can do this without having to copy method implementation code from either of the original classes. Test your code in main method
I did not really understand the problem well, but this is the solution I came with (I know it's not what was asked):
I created 4 classes except main. As Employee has state and behaviour I came up with this code:
public class Employee {
private int ID;
private String Name;
private double salary;
public Employee(int ID, String Name, double salary) {
this.ID = ID;
this.Name = Name;
this.salary = salary;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void startWorking() {
System.out.println("Started Working");
}
}
Class ninja has only behaviour:
public class Ninja {
public Ninja(){}
public void moveNinja(){
System.out.println("Ninja moved");
}
}
Class NinjaEmployee:
public class NinjaEmployee extends Employee implements MyInterface {
public NinjaEmployee(int ID, String Name, double salary) {
super(ID, Name, salary);
}
public void moveNinja() {
System.out.println("Ninja Moved");
}
}
Interface which does not make sense in my code:
public interface MyInterface {
public void moveNinja();
public void startWorking();
}
Main class:
public static void main(String[] args){
MyInterface em = new NinjaEmployee(123,"Sandro",1000000);
em.moveNinja();
em.startWorking();
}
My question is following:
1) Specifically/Technically what was asked in test?
2) What would be correct approach/code for given problem?
Nice question.
The key point of the question is:
we should use one interface.
Ninja class should have some methods (not attributes).
So we should try to use these key point.
I provide a class diagram below:
First of all: We have Employee class and implement it like other simple classes. It has some implemented attributes and classes.
Secondly: We have an Interface named Ninja_Interface that have some method declarations about ninja. (moveNinja1 and moveNinja2)
Thirdly: Ninja Class that implemented (or Realized) Nijna_Interface and have some implementation of any method declarations in Ninja_Interface.
Fourthly: the NinjaEmployee class. It inherited from Employee. So it has all Employee's attributes and methods. Also it implements Ninja_Interface. So it should implements all Ninja_Interface methods declarations. On the other hand, NinjaEmployee have an instance of Ninja (notice that Ninja class implements all Ninja_Interface methods). So, In Ninja_Employee class, in implementation of Ninja_Interface methods, we can use Ninja instance methods to call.
For example some parts of NinjaEmployee is like below code:
private Ninja ninja=new Ninja();
public void moveNinja1()
{
ninja.moveNinja1();
}
public void moveNinja2()
{
ninja.moveNinja2();
}
Main question is: why Ninja class should have only some methods?
It is because of Ninja class is just the implementations of Ninja_Interface methods and there no need to have attributes. So instances of Ninja class are the same. So we can declare Ninja attribute in NinjaEmployee as static attribute.
Finally: we can add some attributes of ninja into NinjaEmployee class too.
I don't know correct answer (task is kinda not very strictly defined, there is some unclear moments), but i would do something like this:
public interface IAmNinja {
public void moveNinja();
}
public interface IAmEmployer {
public void startWorking();
}
public class NinjaEmployee implements IAmNinja, IAmEmployer {
private Ninja _ninja;
private Employer _employer;
public NinjaEmployee(int ID, String Name, double salary) {
_employer = new Employer(ID, Name, salary);
_ninja = new Ninja();
}
public void moveNinja() {
_ninja.moveNinja();
}
public void startWorking() {
_employer.startWorking();
}
}
You cant create 1 object of 2 class es
You can extend class so whenever child class is instantiated it calls parent class constructor
Then You can create object of another class in that constructor
Add employees in array and add option to add employee in ninja? 1.yes or 2.no?
if yes , add to ninja..then in main method print names of ninja using for loop one by one

Basic Java Inheritance

I have a quick question about inheritance. Currently, I am making a simple program that reenacts the process you would do to make a custom block while modding Minecraft with forge.
I have the super class Block:
public class Block {
public String name;
public int id;
public int height = 16;
public Block(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void onRightClick() {
System.out.println("You Right Clicked!");
}
public void setUnlocalizedName(String name) {
this.name = name;
}
}
Then I have made two child classes. They are identical, but here is one just as a reference.
package com.andrewfurniss;
public class GrassBlock extends Block {
public GrassBlock(int id) {
super(id);
this.setUnlocalizedName("DirtBlock");
}
}
The other class is called DirtBlock. When I print out the names it only prints out whatever name I declared second. Why is that?
It is because you set a wrong name in the GrassBlock:
this.setUnlocalizedName("DirtBlock");
Probably a copy-paste mistake.
Maybe you're doing:
public void ToDo(){
Block b = new GrassBlock(0);
b = new DirtBlock(1);
System.out.println(b.getName());
}
??
We need the code you're using to print then name...
I edit, why are you writing "dirtblock" on grassblock ctor?? Maybe it can be that.

How to convert an object from class to superclass

I have to model breakwater that controls permissions in certain coast. My solution implements a class "Ship" and classes "OilShip", "FishingShip" and "CarriageShip", I used inheritance and made
public class OilShip extends Ship{
...
}
public class FishingShip extends Ship{
...
}
public class CarriageShip extends Ship{
...
}
In another class I have Ship ship=new Ship(...); and I'd like to somehow make an Oilship into Ship, i.e.
public Class Abcd{
Ship ship;
public Abcd(OilShip oship){
ship=oship; //*************
}
}
There seems to be a problem with the code, please tell me.
Make sure you call the superclass' constructor inside your subclasses' constructors.
This solution works fine for me:
public class Ship {
private String name;
private int weight;
public Ship(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
class OilShip extends Ship {
private int oilCapacity;
public OilShip(int oilCapacity, String name, int weight) {
super(name, weight);
this.oilCapacity = oilCapacity;
}
}
class FishingShip extends Ship {
private int fisherMen;
public FishingShip(int fisherMen, String name, int weight) {
super(name, weight);
this.fisherMen = fisherMen;
}
}
class CarriageShip extends Ship {
private int containers;
public CarriageShip(int containers, String name, int weight) {
super(name, weight);
this.containers = containers;
}
}
As mentioned before, Java-classes should always be given a name, where the first character is in UPPERCASE and the same with each new word --> CamelCase
You don't need different constructors. The awesome thing behind using inheritance here, is, that no matter what subclass from the superclass "Ship" you put into your constructor in "abcd", it will be accepted:
public class Abcd {
private Ship ship;
public Abcd(Ship ship){
this.ship = ship;
}
}

Defining inner classes in AppEngine Datastore (Objectify)

In AppEngine I need to have an entity Diagram that contains an id, title and a variable list of elements of inner class Box, each one with id and description.
Please find below the definition. However, at time of defining the EntityProxy List getter and setter: "The type java.util.List<Box> cannot be used here".
DIAGRAM.java
#Entity
public class Diagram extends DatastoreObject {
public class Box {
private String boxId;
private String description;
public String get_id() {
return boxId;
}
public void set_id(String boxId) {
this.boxId = boxId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Indexed private String diagramId; // Primary key
#Indexed private String title;
#Embedded private List<Box> boxes;
public String get_id() {
return diagramId;
}
public void set_id(String diagramId) {
this.diagramId = diagramId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setBoxes(List<Box> boxes) {
this.boxes = boxes
}
public List<Box> getBoxes() {
return boxes;
}
}
DIAGRAMPROXY.java
[...]
List<Box> getBoxes();
void setBoxes(List<Box> boxes);
[...]
Your inner class must be static. Nonstatic inner classes have an implicit link to an instance of the outer class, which would be really confusing from the perspective of loading and saving entities to the datastore.
Confusing, you have a Collection<Box> in the Box class? Doesnt sound right.. Anyways the inner Box class must be market static or be moved to a different file. Use the #Embed (version 4.0) annotation on the Box class.
Also, assuming DatastoreObject is the base of all your entities, you can make DatastoreObject as an #Entity and all its sub classes as an #EntitySubClass (index = true). Obviously all sub entities would be be saved under the same 'kind' (DatastoreObject) in the datastore.

Categories