I am using BlueJ for this assignment I have and I have a problem doing this part of the question which is to put decision constructs to ensure that invalid data is not set which I have already tried.In which I placed the if else statement in
the setName portion which does not work. The code will show error if I have put 1 in the GraphicIllustrators main void portion for setName. So where do I need to put the if else statement in the code below.BTW I am using inheritance to do this.So please advise.Thanks!
The coding for the main class:
public class Publishing_Inc
{
private int ID=0;
private String name="-Name Needed-";
private int level=0;
private String jobtitle="-Title Needed-";
private String edit="-Edit Skill-";
public void calculateID(){
int uniqueID;
uniqueID =((int)( Math.random()*10000)+1);
ID = uniqueID;
}
public int getID(){
return ID;
}
public void setName(String d) {
name = d;
}
public String getName() {
return name;
}
public void setTitle(String b){
jobtitle=b;
}
public String getTitle() {
return jobtitle;
}
public void calculatelevel(){
int uniquelevel;
uniquelevel =((int)( Math.random()*3)+1);
level = uniquelevel;
}
public int getlevel() {
return level;
}
public void setEdit(String z){
edit=z;
}
public String getEdit() {
return edit;
}
}
The sub class:
public class GraphicIllustrators extends Publishing_Inc
{
public void displayGraphInformation() {
System.out.println("ID: " + getID());
System.out.println("Name:" + getName());
System.out.println("Job Title: " + getTitle());
System.out.println("Level: " + getlevel());
System.out.println();
}
public static void main (String args[]) {
GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
graphic.setName (" Tim Cook" );
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}
}
public static void main (String args[]) {
GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
if (input instanceof String) {
graphic.setName ( input ); //where input comes from an input box or query or other source.
}
else
{
//alert the user.
}
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}
When you pass an argument to the function setName that isn't a String, Java will throw an exception. You need to cast the object sent to a string. If you want to prevent this, you should check the input before it's send the function setName. This will check if the input is an instance of the Object String, if not alert the user.
Related
Sorry as I know this is obvious but I cant figure it out!
I have a parent class named 'Set', representing a set of a tennis match.
public class Set {
private String set1;
private String set2;
private String set3;
//private Object[] match;
public Set() {
setSet1(set1);
setSet2(set2);
setSet3(set3);
}
public void setSet1(String set1) {
this.set1 = set1;
}
public String getSet1() {
return set1;
}
public void setSet2(String set2) {
this.set2 = set2;
}
public String getSet2() {
return set2;
}
public void setSet3(String set3) {
this.set3 = set3;
}
public String getSet3() {
return set3;
}
public String toString(){
return String.format("set1: %s, set2: %s, set3: %s", set1, set2, set3);
}
}
And a sub class of 'Set' named 'SingleSet', where i try to add the sets into an array named 'game':
public class SingleSet extends Set{
private Object homePlayer;
private Object awayPlayer;
private String[] game;
public SingleSet(Object homePlayer, Object awayPlayer){
super();
game = new String[3];
game[0] = super.getSet1();
game[1] = super.getSet2();
game[2] = super.getSet3();
setHomePlayer(homePlayer);
setAwayPlayer(awayPlayer);
}
public void setHomePlayer(Object homePlayer) {
this.homePlayer = homePlayer;
}
public Object getHomePlayer() {
return homePlayer;
}
public void setAwayPlayer(Object awayPlayer) {
this.awayPlayer = awayPlayer;
}
public Object getAwayPlayer() {
return awayPlayer;
}
public void setGame(String[] game) {
this.game = game;
}
public String[] getGame() {
return game;
}
public String toString(){
return String.format("Player: %s Vs. Player: %s, Single set game: %s, %s, %s", homePlayer, awayPlayer, game[0], game[1], game[2]);
}
}
And this is where I am trying to add the Sets from my parents class into my sub class (this is for FXML, so the code is in my controller):
public void submit() {
SingleSet game1 = new SingleSet(homePlayer1Dropdown.getValue(), awayPlayer1Dropdown.getValue());
game1.setSet1(set1Box1.getText());
game1.setSet2(set1Box2.getText());
game1.setSet3(set1Box3.getText());
System.out.println(game1);
When I print the result, all my values are null. I tried printing them individually and that worked fine, so I know the 'set1Box.getText()' is working fine.
Again sorry for any obvious rookie error!
I've updated my code and same issue. Thank you for the composition answer, I will need it for my project, but this is a IS-A relationship
Make sure that the toString() methods of the following attributes exist and return a correct string.
It seems as if there is no way to get a String from homePlayer, awayPlayer and all indices of game[x].
public String toString(){
return String.format("Player: %s Vs. Player: %s, Single set game: %s, %s, %s", homePlayer, awayPlayer, game[0], game[1], game[2]);
}
May be the Title isn't a specific one, I just don't know how to call it. I will explain you in detail
I have these classes:
public class ChannelComponent {
private String name;
private String mode; //(1P1C / XPXC / 1PXC)
private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>();
private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>();
public ChannelComponent(String name, String mode) {
this.name = name;
this.mode = mode;
}
public boolean canISubscribe(SinkRequiredPort newPort) {
if ((mode.equals("1P1C") || mode.equals("1PXC")) && subscribers.size() < 1) {
subscribers.add(newPort);
return true;
} else if (mode.equals("XPXC")) {
subscribers.add(newPort);
return true;
}
return false;
}
public String getName() {
return name;
}
public String getMode() {
return mode;
}
public void printChannel() {
System.out.println("[" + name + "," + mode + "]" + "\n");
}
}
TestCentralRegistry
public class TestCentralRegistry {
private List<ChannelComponent> channels = new ArrayList<ChannelComponent>();
public void addChannelComponent(ChannelComponent c) {
channels.add(c);
}
public static void main(String... args) {
TestCentralRegistry demo = new TestCentralRegistry();
demo.addChannelComponent(new ChannelComponent("channel1", "1P1C"));
demo.addChannelComponent(new ChannelComponent("channel2", "XPXC"));
}
}
In the TestCentralRegistry class I created 2 channelComponents, these channels I would like to compare their mode value in the method canISubscribe (located in the ChannelComponent class). But how come, I could retrieve the values created in the TestCentralRegistry to read them in the ChannelComponent class?
what am I missing?
Because, from another class TestChannel I'm going to have a ChannelComponent reference, invoke the method canISubscribe
public class TestChannel {
ChannelComponent channelComponent;
public void callSubscribe(SinkRequiredPort newPort){
channelComponent.canISubscribe(newPort);
}
public static void main(String... args) {
TestChannel testChannel = new TestChannel();
SinkRequiredPort sinkPort = new SinkRequiredPort();
sinkPort.setWantsUse("channel1");
testChannel.callSubscribe(sinkPort);
}
}
And I need to compare the values, created in the TestCentralRegistry and TestChannel to see if there is a matching. I know that I still need to add some lines like getting the value from the newPort.getWantsUse(); and compare it with the channelComponent name ... but still I need the value created in the TestCentralRegistry
I hope my question is clear
Any suggestions?
Thank you in advance
Try holding a reference to TestCentralRegistry in ChannelComponent.
public class ChannelComponent {
private String name;
private String mode; //(1P1C / XPXC / 1PXC)
private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>();
private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>();
private TestCentralRegistry testCentralRegistry;
public ChannelComponent(String name, String mode) {
this.name = name;
this.mode = mode;
}
public void registerTestCentralRegistry( TestCentralRegistry testCentralRegistry) {
this.testCentralRegistry = testCentralRegistry;
}
}
Register your TestCentralRegistry as shown below:
public class TestCentralRegistry {
private List<ChannelComponent> channels = new ArrayList<ChannelComponent>();
public void addChannelComponent(ChannelComponent c) {
channels.add(c);
}
public static void main(String... args) {
TestCentralRegistry demo = new TestCentralRegistry();
ChannelComponent cc1 = new ChannelComponent("channel1", "1P1C");
cc1.registerTestCentralRegistry( demo);
ChannelComponent cc2 = new ChannelComponent("channel2", "XPXC");
cc2.registerTestCentralRegistry( demo);
demo.addChannelComponent( cc1);
demo.addChannelComponent( cc2);
}
}
Then, you can retrieve the values created in the TestCentralRegistry by calling testCentralRegistry.getX() from ChannelComponent.
Eclipse keeps informing of an error when I try to implement a counter for a number of instances when called by the constructor. I've been searching on the matter, but the solutions are the exact thing eclipse won't let.
The problem is in Student() { count++; } in the subclass.
Implicit super constructor Dosije() is undefined. Must explicitly invoke another constructor
Main file
import java.util.Scanner;
public class TestDosije {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String jmbg=null;
System.out.println("ime osobe: ");
String ime= in.next();
System.out.println("prezime osobe: ");
String prezime= in.next();
System.out.println("jmbg: ");
while(!(Dosije.jesteJMBG(jmbg =in.next()) )) {
}
String ime_prezime= ime + " " + prezime;
Dosije dosije = new Dosije(ime_prezime, jmbg);
System.out.println(dosije.toString());
System.out.println("broj indeksa: ");
int index= in.nextInt();
System.out.println("godina upisa: ");
int upis= in.nextInt();
System.out.println("studije: ");
int studije= in.nextInt();
Student student = new Student(dosije, index, upis, studije);
System.out.println(student.toString());
System.out.println(student.getCount());
}
}
The superclass
public class Dosije {
private String ime_prezime;
private String jmbg;
public Dosije(String ime_prezime, String jmbg) {
this.ime_prezime=ime_prezime;
this.jmbg=jmbg;
}
public Dosije(final Dosije d) {
ime_prezime=d.ime_prezime;
jmbg=d.jmbg;
}
public String getImePrezime() { return ime_prezime; }
public void setImePrezime(String ime_prezime) { this.ime_prezime= ime_prezime;}
public String getJMBG() { return jmbg; }
public void setJMBG(String jmbg) { this.jmbg= jmbg;}
public String toString() {
return ime_prezime + "\njmbg: " + jmbg;
}
public static boolean jesteJMBG(String jmbg) {
if(jmbg.length() != 13) {
System.err.println("jmbg ima 13 cifara");
return false;
}
for(int i=0;i < jmbg.length(); i++) {
if(!(Character.isDigit(jmbg.charAt(i))) ) {
System.err.println("jmbg nije broj!");
return false;
}
}
return true;
}
}
The subclass of which instances I'm trying to count
public class Student extends Dosije{
private int br_index;
private int god_upis;
private int profil_studija;
private static int count=0;
Student() {
count++; //the devil himself
}
public Student(final Dosije d, int index, int upis, int studije){
super(d);
br_index=index;
god_upis=upis;
profil_studija=studije;
}
public Student(final Student s) {
super(s);
br_index=s.br_index;
god_upis=s.god_upis;
profil_studija=s.profil_studija;
}
public void setProfil(int n) {profil_studija=n;}
public int getCount() { return count; }
public String Studije(int i) {
if(i == 0)
return "Osnovne";
else if(i == 1)
return "MSc";
else
return "PhD";
}
public String toString() {
return super.toString() + "\n" + "broj indeksa: " + br_index + "/" + (god_upis % 100) + "\n"
+ "studije: " + Studije(profil_studija);
}
}
Your Student() constructor doesn't pass compilation since the super class doesn't have a parameterless constructor, so the implicit call to super(); added by the compiler doesn't pass compilation.
You can add a public Dosije() {} constructor to prevent that compilation error.
However, you might want to increment count in the other Student constructors too, in order to count the total number of instances created, regardless of which constructor was used.
Here is what I have so far so as you can see I made a class for the powerup but I just keep getting stuck over and over again and ended up getting frustrated cause I couldn't figure it out myself.
public class Superhero {
private int heroStr;
public int powerUp;
private String name;
public Superhero(String name, int heroStr) {
this.name = name;
this.heroStr = heroStr;
System.out.println(name + " Strength is " + heroStr);
}
public Superhero(String name) {
this.name = name;
heroStr = 10;
System.out.println(name + " Strength is " + heroStr);
}
public int getStr() {
return heroStr;
}
public int powerUp(int powerUp) {
}
public static void main (String[] args) {
Superhero Gambit = new Superhero("Gambit");
Superhero Groot = new Superhero("Groot", 79);
}
}
Here you are:
public void powerUp(int powerUp){
//this.powerUp is the powerUp in your class, the powerUp without "this" is the powerUp given to the method
this.powerUp+=powerUp;
}
All you need now is to change your powerUp method:
public void powerUp(int powerUp) {
this.heroStr += powerUp;
}
and since you instantiated the superheroes, all you need is to call their methods, ex:
public static void main(String args[]){
SuperHero gambit = new SuperHero("Gambit",10);
gambit.powerUp(10);
System.out.println(gambit.getStr()); //should be 20
}
Also, as a side note:
the correct naming convention for variable names is:
Class object = new Class();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why is it when I call System.out.println(classroom.toStringLong()) I get: classroom: a large lecture hall with a null that goes null to null?
The correct output is supposed to be: classroom: a large lecture hall with a door that goes outside to sidewalk
public class Main {
public static void main(String[] args) {
Space classroom = new Space();
classroom.setName("classroom");
classroom.setDescription("a large lecture hall");
Space sidewalk = new Space();
sidewalk.setName("sidewalk");
sidewalk.setDescription("a plain concrete sidewalk with weeds growing through the cracks");
Portal door = new Portal();
door.setName("door");
door.setDirection("outside");
door.setDestination(sidewalk);
classroom.setPortal(door);
System.out.println(classroom.toStringLong());
}
}
public class Space {
private String _name;
private String _description;
private Portal _portal;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDescription() {
return _description;
}
public void setDescription(String _description){
this._description=_description;
}
public Portal getPortal() {
return _portal;
}
public void setPortal(Portal _portal){
this._portal=_portal;
}
public String toString(){
return _name;
}
public String toStringLong(){
if (_portal!= null){
Portal p= new Portal();
p.toStringLong();
String Longcombined=_name + ": " + _description+" with a "+p.toStringLong();
return Longcombined;
}
else{
String Long=_name + ": " + _description;
return Long;
}
}
}
public class Portal {
private String _name;
private String _direction;
private Space _destination;
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDirection(){
return _direction;
}
public void setDirection(String _direction){
this._direction=_direction;
}
public Space getDestination(){
return _destination;
}
public void setDestination(Space _destination){
this._destination=_destination;
}
public String toString(){
String combined=_name+ " that goes "+_direction;
return combined;
}
public String toStringLong(){
Space space=new Space();
String combined=toString() + " to " + space.getDescription() ;
return combined;
}
}
You are creating a new object of space and printing its description which is null
rewrite your toStringLong() method to
In class Space
public String toStringLong(){
if (_portal!= null)
{
// comment this Portal p= new Portal();
_portal.toStringLong();
String Longcombined=_name + ": " + _description+" with a "+_portal.toStringLong();
return Longcombined;
}
else
{
String Long=_name + ": " + _description;
return Long;
}
}
Class Portal ->
public String toStringLong()
{
String combined=toString() + " to " + _destination.getDescription() ;
return combined;
}
Hope this will solve your problem.
When run the this code "Space space=new Space();" inside of the toStringLogn() method, new space object is created and also all instant variables of this object are initialized with their default values. Default value of the String is "null". That is why you get null value when you use this object.
It is better to redefine your toStringLong() as follows,
String combined=toString() + " to " + _destination.getDescription() ;
return combined;