In java please! as soon as pssble, should be simple code :) thank you! Complete
ID: 3811646 • Letter: I
Question
In java please! as soon as pssble, should be simple code :) thank you!
Complete a generic class with the prototype below.
public class Choices{
// a class that represents either None or Some of a type. Add a private field as needed. (None indicates the absence of an item, some indicates the presence of a single item)
public Choices();
//construct a Choice which has a None of a type
public Choices (T item);
//construct a Choice which has Some which is the argument item
public boolean hasSome();
//returns true if the Choices has Some and false if it has None
public T getSome();
//if the Choices has Some, return the item it was constructed with. If the Choices has None, thrown a runtime exception with the message " Choices has None "
public String toString();
// if the Choices has Some, return a string formated "Some(XXX)" where XXX is repalced by the toString() of the item. If the Choices has None, return the string "None".
Explanation / Answer
I have written the Choices class.
I have considered type od some as String. If you want it in some different form you can have the same.
package com.example.Binsearch;
public class Choices {
private String some;
public Choices() {
super();
// TODO Auto-generated constructor stub
}
public Choices(String choice){
some=choice;
}
public boolean hasSome(){
if(null==this.some||"".equals(this.some)){
return false;
}else{
return true;
}
}
public String getSome(){
if(null==this.some||"".equals(this.some)){
throw new RuntimeException("Choices has None");
}else{
return this.some;
}
}
public String toString(){
if(null==this.some||"".equals(this.some)){
return "None";
}else{
return "Some("+this.some+")";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.