It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am striving to print two different values from one array of stored models of computers. At the moment my program print first computer from the index but I cannot get how to print just one of particular model.
This is a fragment of my Main class
ComputerList list = new ComputerList();
Coputer item;
String model;
switch (option)
{
case 'M':
model = Console.askString("Enter model?: ");
item = list.findModel(model);
if (item == null)
System.out.println("Cannot find " + model);
else
item.print("Computer details..." + model);
... and this is my ComputerList class
ArrayList<Laptop> laptops;
private ArrayList<String> models;
public SerialList()
{
laptops = new ArrayList<Laptop>();
models = new ArrayList<String>();
}
public void add(Computer anComputer)
{
laptops.add(anComputer);
models.add(anComputer.getModel());
}
public void print()
{
int nItems = computer.size();
for (int i=0; i<nItems; i++)
{
System.out.println(computers.get(i));
}
public Computer findModel(String aModel)
{
int index = models.indexOf(aModel);
if (index == -1)
return null;
else
return computers.get(index);
}
}
I really struggling resolve this matter for few days but most of tutorials are based on numbers, values etc.
I will be very grateful of any help to this matter.
Regards
When you print an object you get the identifier #222222 etc...
You have to print attributes.
System.out.println(computer.getName + computer.getID);
You need a list iteration through your ComputerList.
ComputerList list = new ComputerList();
for (ComputerList currentComputer: list) {
currentComputer.print();
}
Just memorize the format. Also, I would use a HashMap or a different collection for that one to one String -> Laptop correspondence.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm a Java beginner and I'm currently working on a project that I have almost completed.
I need to remove, modify and extract elements of a list (it's a basic one, though I know there are arrayList(s).) This is driving myself crazy, because I know exactly what I need to do, but I'm not getting what should I need start programming.
package lec05;
import java.util.*;
/**
*
* #author ulacit
*/
public class Lista {
Celda head;
public Lista() {
head = null;
}
public void add(Person aPerson) {
if (head == null) { // list = empty
head = new Celda(aPerson);
} else if (aPerson.getId() < head.getInfo().getId()) { // add element - left
Celda aux = new Celda(aPerson);
aux.setNext(head);
head = aux;
} else if (head.getNext() == null) { // add 1 element - right
Celda aux = new Celda(aPerson);
head.setNext(aux);
} else { // more than 1 - add at the end or in the middle
Celda actual = head;
while (actual.getNext() != null
&& actual.getNext().getInfo().getId() < aPerson.getId()) {
actual = actual.getNext();
}
Celda aux = new Celda(aPerson);
aux.setNext(actual.getNext());
actual.setNext(aux);
}
}
public boolean (int id) {
Celda aux = head;
while (aux != null && aux.getInfo().getId() < id) {
aux = aux.getNext();
}
return (aux != null && aux.getInfo().getId() == id);
}
public Person restore(int id) {
Celda aux = head;
while (aux != null && aux.getInfo().getId() < id) {
aux = aux.getNext();
}
if (aux != null && aux.getInfo().getId() == id) {
return aux.getInfo();
} else {
return null;
}
}
public void remove(int id) {
}
public void modify(int id, String name) {
}
public Persona extract(int id) {
}
#Override
public String toString() {
String s = "List{";
Celda aux = head;
while (aux != null) {
s += aux.getInfo() + ", ";
aux = aux.getNext();
}
return s;
}
}
It's not difficult at all you can..
You have the head of your Lista, what you have is a single linked list
Here is an illustration of a single linked list.
So when you call for example remove(int x), (assume x is the id of a Celda)
Some pseudocode
aux=Head
1. Check if you have aux (aux !=null)
2. Check if the aux has that id
2.1. if not move to the next Celda and start the same question (aux = aux.getNext())
2.2 if it has, you know what to delete, so you have to have a reference to the previous Celda of aux if it exist in this method, so know the previous.getNext() = aux.getNext()
If you can visualize with images is much easier then to code it ;).
Hints:
You shouldn't implement your own list data structure ... unless you are specifically required to do this. It is better to use an existing List type instead; e.g. either ArrayList or LinkedList.
Your code won't compile ...
Develop this incrementally:
Complete and test the add(Person), get(id) and toString() methods before you try to code the remaining methods.
Develop / test the remaining methods one at a time.
Implement your own unit tests. (This is not mandatory, but it will help you test your code systematically. The unit tests don't need to be beautiful ...)
If you are stuck, there are lots of good textbooks on "data structures and algorithms" that explain how a linked list works.
What you have is a single linked list; i.e. nodes in the list have links to the next node ... but not the previous node. The trick for doing operations on a single linked list is that as you are iterating the list you (often) need to keep track of the node that holds the link to the one you are "looking at". For instance, to remove a node from the list you need to be able to modify the node before the current one.
If you are going to add and remove, you need pointers. You keep referencing "getNext" but you don't have a next pointer. Think of a pointer as the connection for each element. If you have 2 elements, how do you know which is first and which is second. You have to have some relation.
Here is a great tutorial to get started: http://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So I have:
private static ArrayList<AbstractAnalyser> analysers = new ArrayList<>();
public static String getAnalyser(String analyser){
if(analysers.contains(analyser)){
return "The full name of the analyser";
}
return null;
}
So what I want is..
If the the arraylist contains the parameter analyser, I want it to return the full name of the object what is in the arraylist.
Let's say these values are in the arraylist:
analyser, method and second. <- random names
If the parameter is analyser and the arraylist contains analyser. The method needs the return that name.
Even when the parameter is "analy".
I would use a Map instead of a List:
private static Map<String, AbstractAnalyser> analysers = new HashMap<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
if (analysers.containsKey(analyserName)) {
result = analysers.get(analyserName);
} else {
for (String key : analysers.keySet()) {
// put the logic to find the one you want here.
}
}
}
return result;
}
But, if you must, you can do it this way if the AbstractAnalyser has a way to give you its name:
private static List<AbstractAnalyser> analysers = new ArrayList<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
for (AbstractAnalyser analyser : analysers) {
// Here's how you look by name
if (analyser.getName().equals(analyserName)) {
result = analyser;
break;
} else {
// put special logic to find the one you want here.
}
}
}
return result;
}
Using the Map is always more efficient when you give the exact name because the lookup is O(1). The List lookup is always O(N).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Im making a program that calculates one's gpa. My variables include the grade A+ etc...
Any help would be greatly appreciated,
Thanks
I would create your own enum class.
enum Grade {
A(4.0, "A"), B_PLUS(3.5, "B+"), B(3.0, "B"), // etc.
F(0, "F");
private float gradePoints;
private String asString;
private Grade(float gradePoints, String asString) {
this.gradePoints = gradePoints;
this.asString = asString;
}
public float getGradePoints() {
return gradePoints;
}
#Override
public String toString() {
return asString;
}
}
This maybe a good candidate for Value Object from Domain-Driven Design.
public class GpaGrade {
private Int32 _inPercent;
public GpaGrade(Int32 inPercent) {
// TODO: check that inPercent is between 1 and 100
_inPercent = inPercent;
}
public Int32 AsPercents() {
return _inPercent;
}
public Double In40Scale() {
// TODO: convert to 4.0 scale
}
public String AsLetterCode() {
// TODO: convert to letter code
}
// TODO: override Equals and GetHashCode
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
got a strange one, i have a variable t i use it in one class, it changes, (e.g. 1 becomes 5) and then i call it from another class to use in that class, problem is t is always 0 when it is passed, what am i doing wrong
here is t in the class where it is edited
public int t = 1; //defualt value for amount of seconds in the future the job should wait untill sent
public int getT() {
return (t);
}
public void setT(int t) {
this.t = t;
}
and this is the class that i am using that calls t from the above class to use:
public class DealyTillPrint {
public int t;
public String CompletefileName;
private String printerindx;
private static int s;
private static int x;
public static int SecondsTillRelase;
public void countDown() {
System.out.println("Countdown called");
s = 1; // interval
t = (t * 60); // number of seconds
System.out.println("t is : " + t);
while (t > 0) {
System.out.println("Printing in : " + t);
try {
Thread.sleep(s * 1000);
} catch (Exception e) {
}
t--;
}
and here is where i set t using a spinner
<p:spinner min="1" max="1000" value="#{printerSettings.t}" size ="1">
<p:ajax update="NewTime"/>
</p:spinner>
How can i call t where the value is passed that is not zero
In DealyTillPrint you declare public int t; That t is different than the t you declare in the first code sample. Since you give it no value, it's default value of 0 is assigned. You are doing nothing to share t in the first sample with t in the second sample.
Change t = (t * 60); // number of seconds to t = (printerSettings.getT() * 60);
You need to get the printerSettings object from web page into your DealyTillPrint object. I can't tell you how to do that looking at the code you've submitted.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public MyObject method1() {
boolean someBoolean = true;
MyObject obj = ...;
if(!someBoolean) method1();
else return obj;
// flow should never come to this statement, but compiler requires this return. why?
return null;
}
why does the java compiler require the final return statement?
-Prasanna
If !someBoolean, then method1 is called, but nothing is returned. So flow totally could end up at that last statement.
Because if your boolean is not true you aren't returning anything. Java requires all methods to return their corresponding value type (in this case, MyObject).
You need to modify your code:
public MyObject method1() {
boolean someBoolean = true;
MyObject obj = ...;
if(!someBoolean) return method1();
else return obj;
}
Originally, your if statement didn't return anything if !someBoolean, it just called method1() and ignored the result.