I have this little bit of code
_grid[4][4].setText(_square[4][4].getTopColor() + ": "
+ _square[4][4].getHeight());
Eventually in my program, the text will change because the value of get.Height will change. Is there a way to write a simple program that sets text based on the coordinates of the multidimensional array?
So if the method was called, updateText, I could just do _grid[4][4].updateText(); and it would be the same as the code above. Or if I did _grid[0][12].updateText(), it would do the same as this:
_grid[0][12].setText(_square[0][12].getTopColor() + ": "
+ _square[0][12].getHeight());
It's easy enough to refactor that line into a method in the same class.
private void updateText(int row, int col) {
_grid[row][col].setText(_square[row][col].getTopColor() + ": "
+ _square[row][col].getHeight());
}
If you want to make it a method of the grid items, you'll have to give more details. Do the items in the grid know about their corresponding _squares?
Related
Im attempting to remove an object form an array list but if fails everytime.
Im using a function to add and a seperate one to remove. The add function is working but the remove is not. I cant see where it is going wrong and failing.
public void unregister( Basics classToRemove){
if(checkIsRegistered(classToRemove)){
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
System.out.println("You have unregistered from " + classToRemove.className + " class.");
setTotalTuition();
} else{
System.out.println("\nYou are not currently registered for " + classToRemove.className + " class.\n");
}
}
public void register(Basics classToAdd){
if(!checkIsRegistered(classToAdd)){
getClassList().add(classToAdd);
System.out.println("You have registered for " + classToAdd.className + " class.");
setTotalTuition();
}else{
System.out.println("\nYou are already registered for " + classToAdd.className + " class.\n");
}
}
I have attempted using this.arrayList to remove the object and using getters to get the object and remove it.
You can not just print a class-object with System.out.printline. I assume you want to print the attributes of the class, however you are printing the memory address of the object.
Additionally, this doesn't make sense to me:
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
You can't access and print an Object you just removed.
If this is not helpful, consider providing a minimal example and explaining what exactly is not working.
I am creating a JavaFX Seating Arrangement APP
#FXML
void addStudentClicked(ActionEvent event) {
String str = studentname.getText();
Random random = new Random();
int randomInt = random.nextInt(9);
if(randomInt == 0){
if(str.isEmpty()){
errorLabel.setText("ERROR");
}
else{
studentname1.setText(studentname.getText());
student1.setFill(studentcolour.getValue());
}
}
else if(randomInt == 1){
if(str.isEmpty()){
errorLabel.setText("ERROR");
}
This is my code that so far will take the user input and apply it to a random seat, it continues with else if's for each seat is there a cleaner way for me to do this then with all the else if's and how can I check to see if the seat is already filled so I am not filling the same seat twice?
Also is there a way to check if the colour has already been taken?
An interesting alternative to using the random function is to put all your choices in a List and then apply the Collection method shuffle(). Then, you can iterate through the list. This ensures that each option is chosen once and only once.
I haven't looked closely enough at the code to tell if a switch/case construct would be a better choice, but that is always something to consider.
Here is a similar (deliberately not the same) example just using the console.
I leave it to you to create a GUI.
Hints
Writing GUIs is not the same as writing console apps because GUIs are event-driven, so you need to get rid of some loops and act on events.
You can use ObservableLists to have list changes reflected in the UI.
When you remove something from an ObservableList, it will no longer be in the list, so if it is bound to something in the UI, it will no longer be there
e.g. if it is a color selection list and a color is removed from it, the color will no longer be available for selection.
Shuffle a list, then pick from the shuffled list
This is the same idea as physically shuffling a deck of cards, then grabbing the cards in order from top to bottom.
The basic idea is that one way to solve this problem is to apply:
Random shuffling of an array
You could also study:
Unique (non-repeating) random numbers in O(1)?
https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
But you don't need to write the shuffle algorithm yourself, you can just use the one already implemented in the java collection library.
Collections.shuffle(seatNumbers);
Example app
Console app-based example.
I'm sure the example uses some concepts you aren't familiar with, and if you use them, you should understand them or do them a different way. In particular:
Colors could be selected from a pre-defined array (like the names are) rather than using an interpolated generator.
An array of seat numbers could be generated using a loop adding to a list rather than a stream adding to a collection in a lambda.
Try running the sample application and look at the output to see what it did, then try to think about how you might apply similar concepts to your program.
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MusicalDragons {
public static final int NUM_ROUNDS = 3;
public static void main(String[] args) {
MusicalDragons game = new MusicalDragons();
for (int i = 0; i < NUM_ROUNDS; i++) {
System.out.println("Round " + (i+1));
System.out.println("=========");
game.play();
System.out.println();
}
}
private final List<String> dragons;
private final List<Color> palette;
private final List<Integer> seatNumbers;
private final int numSeats;
public MusicalDragons() {
// generate a list of names.
dragons = Arrays.asList(
"Oyositatu (Old + Dragon)",
"Tamasipionosi (Soul + Master)",
"Timatamï (Crossroad + Snake)",
"Tômiarôzi (Wealth + Master)",
"Ararepemi (Hail + Snake)",
"Umasumera (Horse + Emperor)",
"Umitatu (Sea + Dragon)",
"Akitatu (Autumn + Dragon)",
"Tawayatatu (Weak + Dragon)",
"Amoritatu (Descend From Heaven + Dragon)"
);
numSeats = dragons.size() - 1;
// generate a list of colors.
palette = new ArrayList<>();
for (int i = 0; i < numSeats; i++) {
palette.add(
Color.RED.interpolate(
Color.VIOLET,
i * (1.0 / (numSeats - 1))
)
);
}
// generate a list of seat numbers.
seatNumbers =
IntStream.range(0, numSeats)
.boxed()
.collect(
Collectors.toCollection(
ArrayList::new
)
);
}
private void play() {
// randomize the order of everything.
Collections.shuffle(palette);
Collections.shuffle(seatNumbers);
Collections.shuffle(dragons);
// display the seat assignments.
for (int i = 0; i < numSeats; i++) {
System.out.println(
dragons.get(i) + "\n" +
" gets seat #" + seatNumbers.get(i) + "\n" +
" colored " + palette.get(i)
);
}
// hmm, one dragon missed out . . .
System.out.println();
System.out.println(
dragons.get(numSeats) +
" does not get a seat and is very angry."
);
}
}
Output
Round 1
=========
Tawayatatu (Weak + Dragon)
gets seat #1
colored 0xf64884ff
Tamasipionosi (Soul + Master)
gets seat #4
colored 0xff0000ff
Oyositatu (Old + Dragon)
gets seat #7
colored 0xfd0e1aff
Ararepemi (Hail + Snake)
gets seat #0
colored 0xf074d4ff
Umasumera (Horse + Emperor)
gets seat #8
colored 0xfb1d35ff
Amoritatu (Descend From Heaven + Dragon)
gets seat #3
colored 0xf73a6aff
Timatamï (Crossroad + Snake)
gets seat #2
colored 0xf4579fff
Umitatu (Sea + Dragon)
gets seat #5
colored 0xf265b9ff
Tômiarôzi (Wealth + Master)
gets seat #6
colored 0xf92b4fff
Akitatu (Autumn + Dragon) does not get a seat and is very angry.
My need is to read the color of a text with PDFlib TET.
As a basis I'm using this PDFlib example: https://www.pdflib.com/tet-cookbook/tet_and_pdflib/search_and_replace_text/
Before both result.add(new rectangle(...)) calls I'm trying to read the color like this:
String csname = tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/name");
if ("Separation".equals(csname)) {
String type = tet.pcos_get_string(doc, "type:colorspaces[" + tet.colorspaceid + "]/colorantname");
System.out.println(type);
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/colorantname"));
}
}
Unfortunately tet.colorspaceid is always 0.
But the correct colorspaceid is 6 (with "correct" = the index of the color the text actually is written with). I know the indexes because I iterated over all colorspaces like this and for i=6 the system prints the name of the intended color:
String type = tet.pcos_get_string(doc, "type:colorspaces[" + i + "]/colorantname");
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + i + "]/colorantname"));
}
What do I need to do for tet.colorspaceid being the id of the colorspace of the currently found word fragment?
Or am I completely wrong and TET reads the color somehow else?
Found it - the solution is method print_color_value in this example: https://www.pdflib.com/tet-cookbook/text/glyphinfo/
Just copy method print_color_value, return csname (or colorantname in the if blocks) and rename the method to e.g. getColorValue.
If needed throw away the formatter stuff.
I've decided to programm a search system for finding students and teachers in a school via GUI. It is an OOP and need some tweaking here and there, but there is one issue which doesn't seem logical to me. When I'm searching for a teacher, I have to type there name or surname into a JTextField and press the Search button which runs a method that loops through an ArrayList of teacher-objects and checks if their names match with the one in the Textfield. Then it checks if these teachers have multiple subjects and grades and it goes through nested if-statements. After the teacher is found, their information is displayed on a GUI with several Texfields. Theoretically if the name I typed into the TextField doesn't match one from the teacher objects, a Error Message should pop-up saying the teacher I'm looking for isn't in the system. But even though I type in the correct name and get all the information displayed, it sends me to the Error Message everytime. I tried to fix it with a break statement but that didn't work either. Can someone please help me with this.
Here is the code I'm talking about:
public void lehrerSuche()
{
String lehrername = tfSuchfeldLehrer.getText();
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++)
{
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername))
{
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
break;
}
else
{
switchPanels_3(panelErrorLehrer);
}
}
}
I've uploaded my project to GitHub. Methods and variables are written in German, so I'm really sorry if you can't understand what I have written. If u have questions please hit me up. I use Eclipse to code.
This link should direct you to my GitHub:
https://github.com/Gonzo-CR/Home-Projects.git
If the link doesn't work, look for Gonzo-CR on GitHub and check out my Home-projects repository where I uploaded all the files.
For better undestanding these are the Object oriented classes:
Person(Abstract)
Schueler
Lehrer
Fach
Schulklasse
Spezial
Sprecher
GUI classes:
Suchsystem
Testdaten(A class which generates all my objects)
The problem is likely that if td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) is not true the very first time the loop runs, switchPanels_3(panelErrorLehrer); will be triggered - regardless of whether the condition is met on a later iteration of the loop.
What you need is to check a sentinel value after the loop finishes - e.g.:
bool lehrerGefunden = false;
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++){
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername)){
//etc.
lehrerGefunden = true;
break;
}
}
if(!lehrerGefunden){
switchPanels_3(panelErrorLehrer);
}
That way, you check every entry in the list before deciding whether to show the error.
State st = sc.que.remove();
System.out.println(st.getMoves() +" - Goal: " + Arrays.toString(st.getGoal()) + "- Puzzle: " + Arrays.toString(st.getPuzzle()));
State ss = new State(st.getPuzzle(), st.getSpace(), st.getMoves(), st.getGoal());
ss.moveUp();
System.out.println(st.getMoves() +" - Goal: " + Arrays.toString(st.getGoal()) + "- Puzzle: " + Arrays.toString(st.getPuzzle()));
Basically I have one state, I print off it's value. Then I create a second state using the value of the first to create an exact copy. I modify the second state using the method moveUp() which swaps a couple elements in an array withine the State. Then we I reprint the value from the first state the one that was not changed they are different.
What are the members of State? If they are objects, you don't copy them by using getMember() but passing a reference to them to the second constructor. If you then call a method that changes a member of the first object, the identical member is changed in the second object, too.