#I need help with a Project for class
1 messages · Page 3 of 1
if(obj == this) {
return true;
}```
although you shouldnt return false if its not
not something to worry about rn. all you need to worry about is the serials
nope
that would be comparing the appliance itself to a serial number
rather than comparing 2 serial numbers
the problem is, you cant access SRN from obj
obj.SRN does not work
otherwise you could just do this.SRN == obj.SRN (but using equals instead of ==)
maybe i don't need to access it
then i don't know
thats the only way you'll be able to compare the serials
you need to introduce a new variable
remember Appliance o?
we need to somehow access the serial number from obj
but the variable obj is Object
the actual value is Appliance, the variable obj will reference an Appliance object
but obj... its an Object
so, introduce a new variable, and assign obj to it
Appliance app = obj;```
Java wont let you do that directly, you gotta "cast"
casting is just the parenthesis right
all this does is give you access to SRN
obj always had SRN. but since the variable datatype was Object, it wasnt visible
so you created a new variable, with the proper datatype
now you can access SRN
you still gotta check if the serials are equal
but now you have access to SRN, with your new variable
btw, once you're finished, make sure you look over this code like 20 times, make sure you understand every last drop
because these struggles are only temporary, if you take that route
otherwise, these struggles can last years
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;
}
else{
return false;
}
Appliance app = (Appliance) obj;
if(obj.equals(app))
return true;
}
so you just threw the code at the bottom of the method
oh right the brackets
you need to think of the if, the branching thats going on
you should only compare if its an appliance
and you shouldnt just return true, you should only return true if the SRNs are equal
oh i think i wrote it wrong
at least this part i need to know
Appliance app = (Appliance) obj;
did i properly cast it
yup
wait
yea
right
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;
}
else{
return false;
}
Appliance app = (Appliance) obj;
if(obj.equals(app))
return true;
}```
Detected code, here are some useful tools:
its currently that rn
step through it
nah, step by step man, break it down, line by line
lets pretend someone calls this method. they pass in a Dog
first thing that happens? java if(obj instanceof Appliance)
ok if obj in part of appliance, return true
but its a Dog being passed in
then false
wait
now, pretend an Appliance is passed in
hm?
no hold on i migth've mixed it up
line by line, think of it, don't jump to conclusions
u said this
yes
obj = new Dog(), it references a dog. what happens in your code? line by line
i know its not
your understanding is the problem
thats why we are stepping through
first thing that happens, first line
yup, easy, method ends
and then it ends
now, obj = new Appliance()
first thing that happens?
it gets checked, still
if(obj instanceof Appliance)```
but now its actually true
so what happens next?
the idea is to make it go onto the rest of the code
Please press ctrl+a and then ctrl+i in your java files. It will format them for you
yeah, but what does the code say? what actually happens?
now this is true. what happens next?
what does the code say?
i think it goes into that block of code
it does. what does that block do?
make a new appliance
does it?
appliance app
whats in the block?
if(obj instanceof Appliance) {
// it is an appliance
}```
what does your code do if its an appliance?
return true
yup, method ends
so if its a dog or something, it goes to else, returns false, method ends
if its actually an appliance, it returns true, method ends
if out of scope
instead of just returning true, you want that code to run
u were trying to say it would never go in because its outa scope?
so put that code in the block
yes
right now, the code you put at the bottom, its unreachable
because if its not an appliance, it returns false
if it is, it returns true
theres no way for the code at the bottom to be reached
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;
Appliance app = (Appliance) obj;
if(obj.equals(app))
return true;
}
else{
return false;
}
}```
when do you want the code on the bottom to run?
yes
exactly
you also need to fix your braces
if(...) {
} else {
}```
you have ```java
if(...) {
else {
}```
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;
Appliance app = (Appliance) obj;
if(obj.equals(app)){
return true;
}
else{
return false;
}
}```
you are closer
read it line by line, again
pretend obj is an appliance
what happens?
start at the very first line
"it checks ..."
ok so i'm trying to to compare obj that is an appliance to another one
"then ..."
app
if its an applalince
and it is
thats what you wrote
if(obj instanceof Appliance)
return true;```
the code is going to do what you tell it. and thats what you told it
see the problem?
because you told it to return
exactly
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
Appliance app = (Appliance) obj;
if(obj.equals(app)){
return true;
}
else{
return false;
}
}```
ok so if its an appliance
it'll do Appliance app = (Appliance) obj;
this
then check with it
or so if(obj.equals(app)){
i should if i wrote that right
Once you know obj is of type appliance why do you make app and then compare it to obj
to see if it equal each other
you are so much closer now
question is, do you understand what your code is doing?
line by line
alright can explain this one to make sure
Appliance app = (Appliance) obj;
this is casting to applaince
casting the obj
but i guess its going in app
so i'm comparing the wrong thing
am i just comparing the same thing
Object obj and Appliance app are variables
but hasn't obj been cast
to appliance
does that not mean it becomes a appliance too through app?
its already an appliance. you gotta think of "data/values" and "variables" separately
Object app = new Appliance();
Object dog = new Dog();
Object tv = new TV();```
so what ur saying is i'm comparing a vairble to a datatype?
In java, every class/object is considered of the type Object
You can pass a Double, String or whatever object you want into your equal function
But they will still have the type they originally had
It doesn't change
obj will remain the type you passed in, so all the methods will be the same
im saying obj is already an appliance. we just need to view it differently. instead of viewing it as an Object, you needed to view it as an Appliance
Once you know it's of type appliance, you already know it will have the same fields and methods
okay
Object obj = new Appliance();
// cannot access Appliance things
Appliance app = obj;
// same data, but now seen as an Appliance```
yeah, it is
they reference the same data
but i can't just right obj a can i?
not sure what you mean
Object obj = new Appliance();```
this thing
is this how u would write it
if u wanna declare a "appliance" varible
its abstraction
ok
if i write
obj x.equals(appliance y)
that wouldn't be right would it?
if they can declare the same variles
But I’m asking I can’t just write obj x right?
Or can I
I understand it’s na appliance now
It can declare a appliance variable, a serial number
But it that how u would write it?
btw i just realized u were talking about how this wasn't an appliance but in the second line it's seen as it now
thxs btw
I'll continue this later I've really gotta go now since i got school tmw
I'll be back
before i go though i was gonna say
after obj can now act like a appliance, it can make varibles like one, but appliance cant do that with object cause its not vice versa and sp since obj can make appliance varibles, u can decalre obj (some varible) and appliance (some varible) and see if they equal each other, just i'm not sure abotu the right syntex
@left tartan
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍
what have you done until now?
do you still need help
Yes I saw that for the equals, we could just use the code the professor provided for that part
This is what I have so far
btw
i got the sort thing fixed
it was just a cause of a simple curly bracket
now all the letters can be in order, just that now null prints in between 💀 so that might be cause of the mutple arrays
I uploaded your attachments as Gist.
and this my appliance class
public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() {
return SRN;
}
public int compareTo (Appliance x) {
return this.SRN.compareTo(x.SRN);
}
@Override
public boolean equals(Object other){
return other != null && getClass() == other.getClass() && SRN.equals(((Appliance) other).SRN);
}
public char convert(){
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
so far this is what i have
But i'm in class rn so i can't work on it atm so i'll be back once i'm home
oh right for the equals
lemme show the slide
btw I think I might change the 3 separte arrays for each
cause i could maybe just check the letter in just one array and make that go in one the 3 collums in the gui somehow
or at least i hope i can
thats where i'm at now, i'll be back later
@left tartan Do you know what are you doing here?:
Appliance twoDArr[][] = getTableArray();
for (var i: twoDArr) {
for (var j: i) {
System.out.println(j);
}
}```
This is where you get nulls right? It's easy to know
please use descriptive names, too. short-hand names caused some confusion with your last problem, confusing o and y
descriptive names will make it easier to help, and also show us that you actually understand your code
And make you able to understand your code if you didn't touch it for weeks or months
because it provides better context
It was there to check how the whole array looked ye, I was explaining what I got up to in a hurry so I didn’t notice
That happens because the 3 arrays are of size (all the serials), but it only includes the serial of one type, each one
Ye it has the space of all of it together in each
anyways, I don't think it matters a lot, because the assignment says it's ok to have partially filled arrays
Oh yea
But if I’m to use the 2d array now to print that onto the gui
Actually maybe not
Cause if it checks just the letter
It should put it in one of the three boxes on there
I don't understand
And null just well disappears
Cause the gui is suppose to have three Collums separating each with their letter
yes
And so if I make it check
I can just tell it which text panel to go to
Or that’s the plan
The table does that for you, you just have to set the column name
let's go to the constructor
I’ll be back in a few hours man I have calc rn 😭
ok
Ye I shall be back
I might made the getTableArray wrong, as I made, per column and not per row
ping me when you're ready
@left tartan are you free?
i have a class online rn
it ends at 8:30
actually that doesn't help if i say that cause diff timezones
bascially ends in an hour and 10 minutes from now
ok
alright i'm back
rn what i have legt to do is print the appliances onto the gui i believe
@uncut marsh
as of rn now this is what i got
I'm a bit busy rn
but basically
You create a JScrollPane
add the table in the scroll pane
then add the scroll pane in the frame
I'll explain that later
is that in the table one
static Appliance[][] getTableArray() {
SelectionSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
int r = 0, d = 0, m = 0;
for(int i = 0; i < arr.length;i++){
if (arr[i].convert() == 'R') {
refrigerators[r] = arr[i];
r++;
}
if (arr[i].convert() == 'D') {
dishwashers[d] = arr[i];
d++;
}
if (arr[i].convert() == 'M') {
microwaves[m] = arr[i];
m++;
}
}
return new Appliance[][]{refrigerators, dishwashers, microwaves};
}```
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void fillGUI() {
}```
or should i put that in the empty method
I'll go and work on it then and see what i can do
the table one is wrong:
what it does is to send an array of three arrays, each one with one type.
But the table will read as, "Ok. Let's take each array and convert it to a row!"
that will make it as a single row of refrigerators, a single row of microwaves, etc.
we will change it to modify the 2 dimensional array directly instead of adding the three arrays
oh alright
Ok I'm free now
can you use setLocationRelativeTo(null)?
it will show in the center
my prof only used the setLocation
ok
I recommend putting setVisible last
alright
yea
- we make the frame a private static variable like arr
- we pass frame to fillGUI
both work
second one
mostly cause i already put frame in the parameters so mgiht as well
public static void fillGUI(JFrame body) {
}```
alright so
call fillGUI before setVisible()
in makeGUI
then let's create a JTable
oh before that
create an array of Strings
for the column names
can't i just append to make the column?
how?
i'm not too familiar i was trying to tell from the slides
i'll send what he sent
Oh we aren't using containers 💀
we create a String[]
no
💀
heard it from u the first time
well we have to do that way 😭
ye
make the layout thing with 3 columns
empty
and show your progress
we will fill it after
oh right i gotta do 1,3
for 3 columns
okay
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
body.setLayout(new GridLayout(1,3);)
}```
you named it body
oh right
gotta follow this
actually it might've been that
cause that adding text
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
myContentPane.add(Refeg, BorderLayout.EAST);
myContentPane.add(dishWash, BorderLayout.WEST);
myContentPane.add(micro, BorderLayout.CENTER);
body.setLayout(new GridLayout(1,3);)
}```
i thought gridlayout was the other alternative
theres a few things here
this east west stuff
first, i would never recommend relying on the existing content pane. that exists for formality reasons
introduce your own panel, then set that panel as the content pane
otherwise you can wind up with some funky results, such as when using BoxLayout
if you need gridlayout instead of borderlayout, sure
just make sure you understand what they do
they do the same thing right?
but they both give u mutiple panels
so does GridBagLayout
well of course
there are tons
java got alot of stuff
This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components
It says grid layout
tons of layouts
in your assignment
but i would only need to say it once right
ah
if you cant handle requirements from a school assignment, you will fail with real-world assignments
so i just misread it
time to put more priority on that
yea
ok so with gridlayout
i just need to say it once so thats means i just write java body.setLayout(new GridLayout(1,3);)
instead of contentpane border
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
ok so now i do the adding
setLayout should be last
i think
I never used that
right
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
for(int i =0;i < arr.length;i++){
}```
ok so now since we have the different panels
before adding
yea
let say you have this text area:
you append "A": A
oh yea so it puts words in the textarea right
okie dokie
there are two ways
- we use the the if arr[i].convert() == 'R' logic there and delete the getTableArray
- we use the getTableArray
wait is that jtable thing
since your professor don't want you to use the JTable we won't need the table array
if he didn't ever mention in to us, i assume we can't use it
ah ye
so first option it is
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
for(int i =0;i < arr.length;i++){
if ( (arr[i].convert() == 'R')){
}
if ( (arr[i].convert() == 'D')){
}
if ((arr[i].convert() == 'M') ){
}
}
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
should be before the myContentPane.add()
oh yeah
there we use the logic that we applied in the getTableArray function
if (arr[i].convert() ==
etc
static Appliance[][] getTableArray() {
SelectionSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
int r = 0, d = 0, m = 0;
for(int i = 0; i < arr.length;i++){
if (arr[i].convert() == 'R') {
refrigerators[r] = arr[i];
r++;
}
if (arr[i].convert() == 'D') {
dishwashers[d] = arr[i];
d++;
}
if (arr[i].convert() == 'M') {
microwaves[m] = arr[i];
m++;
}
}
return new Appliance[][]{refrigerators, dishwashers, microwaves};
}```
so the if there
yeah
3 ifs then
we will change the content of the ifs
if (arr[i].convert() == 'R') {}
if without the body, but with the statement
@left tartan
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
for(int i =0;i < arr.length;i++){
if ((arr[i].convert() == 'R')){
refeg.append();
}
if ((arr[i].convert() == 'D')){
dishWash.append();
}
if ((arr[i].convert() == 'M') ){
mircro.append();
}
}
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
it'll place it in a certain pane
yes
what is concat mean
refer.concat(arr[i]+"\n")
it will add it to the textArea
wait
let me check the presentation
I mean .append not .concat
oh ye
my bad
alright so
in the ifs
once it does the condition
the textarea.append
textarea being the names
the column yes
each textarea is a column
if its a 'R', we add it to the refeg column
you put .append()
but .append() what
yes
i gotta use the array somehow
*use
"I gotta sue the array somehow" sounds funny
lol
ok but in the table thingy we had it return a 2d array
hold on
our 3 separte arrasy we made
that still stays there or no?
we can delete the table thingy
we won't use it
everything?
static Appliance[][] getTableArray() {
SelectionSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
int r = 0, d = 0, m = 0;
for(int i = 0; i < arr.length;i++){
if (arr[i].convert() == 'R') {
refrigerators[r] = arr[i];
r++;
}
if (arr[i].convert() == 'D') {
dishwashers[d] = arr[i];
d++;
}
if (arr[i].convert() == 'M') {
microwaves[m] = arr[i];
m++;
}
}
return new Appliance[][]{refrigerators, dishwashers, microwaves};
}```
the function getTableArray yes
alright
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
for(int i =0;i < arr.length;i++){
if ((arr[i].convert() == 'R')){
refeg.append(arr[i].toString());
}
if ((arr[i].convert() == 'D')){
dishWash.append(arr[i].toString());
}
if ((arr[i].convert() == 'M') ){
mircro.append(arr[i].toString());
}
}
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
you need to append something
yeah
right
the srn in the array
public static void main (String[] args) {
readfile();
SelectionSort();
for (int i =0; i < arr.length; i++) {
Appliance a = arr[i];
System.out.println(a);
}
Appliance twoDArr[][] = getTableArray();
for (var i: twoDArr) {
for (var j: i) {
System.out.println(j);
}
}
makeGUI();
}```
so the twoDArr
remember we used arr[i].convert()
oh
@Override
public String toString() {
return SRN;
}
public int compareTo (Appliance x) {
return this.SRN.compareTo(x.SRN);
}
@Override
public boolean equals(Object other){
return other != null && getClass() == other.getClass() && SRN.equals(((Appliance) other).SRN);
}
public char convert(){
return SRN.charAt(0);
}```
yeh
oh so the convert
i need to ask because i wanna be sure
we gotta append the serieal number in the panel
yes
that serial number will go through convert and gime the first letter
and in the if statement we cheek what letter it is
but that whole serial number
how do we retrive it
arr[i].toString()
ohhh
ok that actaully makes sense
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void fillGUI(JFrame body) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
body.setLayout(new GridLayout(1,3);)
for(int i =0;i < arr.length;i++){
if ((arr[i].convert() == 'R')){
refeg.append(arr[i].toString());
}
if ((arr[i].convert() == 'D')){
dishWash.append(arr[i].toString());
}
if ((arr[i].convert() == 'M') ){
mircro.append(arr[i].toString());
}
}
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
its red
what
ok lemme check what to import
it's body not gui
what
they are part of swing
and they're added to a JFrame
yes
but i have it
import java.awt.GridLayout;
import java.awt.TextArea;```
we don't need the extends JFrame
alright all colorful
try it
let's see
It could be the content pane or the JFrame
that isnt saving
damn
I see what's the issue
the name
not body
change it to gui
and the issue magically disappears
change every body to gui
yes
strange
can you restart eclipse
alright
to see if the problem fixes
I uploaded your attachments as Gist.
public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() {
return SRN;
}
public int compareTo (Appliance x) {
return this.SRN.compareTo(x.SRN);
}
@Override
public boolean equals(Object other){
return other != null && getClass() == other.getClass() && SRN.equals(((Appliance) other).SRN);
}
public char convert(){
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
ok lemme restart
ok i ran it again
wait
I now checked your code
you didn't call fillGUI
you have to call it before setVisible
@left tartan
does it work now
not there
in makegui
yes
you have to pass the JFrame 💀
yes
just add a new line character
in the append
you have
arr[i].toString()
to add it
use +
to add strings
in each of the ifs?
no
in the append
arr[i].toString() + '\n'
public static void fillGUI(JFrame gui) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
gui.setLayout(new GridLayout(1,3));
for(int i =0;i < arr.length;i++){
if ((arr[i].convert() == 'R')){
refeg.append(arr[i].toString()+ "\n");
}
if ((arr[i].convert() == 'D')){
dishWash.append(arr[i].toString() + "\n");
}
if ((arr[i].convert() == 'M') ){
micro.append(arr[i].toString()+ "\n");
}
}
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
Detected code, here are some useful tools:
like that?
another thing
ye
add a safety check in the convert() method
let's say
oh we got a line
the line being
""
let's try to access the first element
StringIndexOutOfBoundsException
oh that sounds familar
check if the SRN length is 0
before trying to access charAt(0)
so we are in the convert() method
public char convert(){
if (SRN.length() == 0) return X;
return SRN.charAt(0);
}```
check first if its length is 0
before returning
with an if?
if (SRN.length() == 0)
like that
yes
lets return a character that we will use as "lol not found"
it could be space
X
N
etc
X then
yeah return 'X'
like literally?
public char convert(){
if ((SRN.isEmpty()) return X;
return SRN.charAt(0);
}```
interesting
I think it's done
ok if it equals 0, that means the string isn't a word?
could u explain this line again
yeah
an empty String
we can also use
.isEmpty()
literally does the same thing
should i write it then
oh its separte
yeah you can change it
does that still go in covnert
you can use
if (SRN.isEmpty())
or the one before
choose the one you prefer
well then use it
does this go in its own method
public char convert(){
if ((SRN.isEmpty()) return X;
return SRN.charAt(0);
}```
or its ok like that
its ok
isEmpty() is part of String
SRN is a String
check the fillGUI function
it doesn't have a case for X
so it won't add it to any column
it will be in the array though
remember the syntax
String str = "X";
char chr = 'X';```
you're returning a char, not a String
so it needs simple quotes
alright
ok good
but now one more problem
i can edit this panel
i don't think i'm supose to be able to do that
what do i do about that
The assignment doesn't explicitly say you can't edit it ¯_(ツ)_/¯
lol
thats true lol
but i doubt he'd be okay with a file that isn't protected or something
i was trying to do
.setEditable(false);
but its just red 💀
the three?
oh the panels
i put it there
public static void makeGUI() {
JFrame gui = new JFrame();
gui.setEditable(false);
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocation(100,100);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fillGUI(gui);
gui.setVisible(true);
}```
do you know in what belongs setEditable?
gui is a JFrame 😭
its just the empty frame
it doesn't belong there
ohh
gui has a container
no wonder it wasn't in this presentation
how did you found out about setEditable
yes but
set editable is a TextArea method
not JFrame
or Container
refeg.setEditable(false);
dishWash.setEditable(false);
micro.setEditable(false);``` 😭
ok right those applainces are ebing printed
is only makes sense to write don't edit after u put them in a panel
or while u put them there
yes
and because those were made from the datatype textarea
doign .not edit and stuff
since tis part of it
works
ok
ok i shouldn't write that when they are being declared right
while they are being added?
public static void fillGUI(JFrame gui) {
Container myContentPane = gui.getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
gui.setLayout(new GridLayout(1,3));
for(int i =0;i < arr.length;i++){
if ((arr[i].convert() == 'R')){
refeg.append(arr[i].toString()+ "\n");
}
if ((arr[i].convert() == 'D')){
dishWash.append(arr[i].toString() + "\n");
}
if ((arr[i].convert() == 'M') ){
micro.append(arr[i].toString()+ "\n");
}
}
myContentPane.add(refeg.setEditable(false))
myContentPane.add(dishWash.setEditable(false));
myContentPane.add(micro.setEditable(false));
}```
why
@left tartan setEditable is a void method
you can't add nothing to the pane
they should be outside
doesn't that mean
i just the names
and then put dont edit
after everything in that method
refeg.setEditable(false);
dishWash.setEditable(false);
micro.setEditable(false);
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);```
these swing things can get confusing. Nemux seems to be on a good track
you guys lmk if you need extra input
now it's done right
A question
does your prof ask you how you do that x in class?
anyways you have to understand
x can be anything
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);```
oh u meant the other one
public char convert(){
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}```
anyways I meant
that you have to understand what you doing here so that you can apply your knowledge later
e.g if you have an exam
yea
ask me any questions about your code
ok lemme put it all here again
if you don't understand what its doing or how
I uploaded your attachments as Gist.
public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() {
return SRN;
}
public int compareTo (Appliance x) {
return this.SRN.compareTo(x.SRN);
}
@Override
public boolean equals(Object other){
return other != null && getClass() == other.getClass() && SRN.equals(((Appliance) other).SRN);
}
public char convert(){
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
Detected code, here are some useful tools:
gimme a minu
i ma read it all
ok so
starting with the main
public static void main (String[] args) {
readfile();
SelectionSort();
for (int i =0; i < arr.length; i++) {
Appliance a = arr[i];
System.out.println(a);
}
makeGUI();
}```
its reading the file to get the serial numbers
putting them in array
yes
*Alphabetical
right yeah
hmm
selectionsort only sorts them in that order
there is still checking the letter
and put them in each panel
so
actaully lemme check where
in fillgui
which is called in makegui
and thats called
in the main
after the for loop
yes
so you don't actually need it
so thats why it still printed the numbers there
oh alright
can i keep it there
yea
ok cool
i guess lets go over the count part again
public static void readfile (){
String filename = "Project1.txt";
TextFileInput in = new TextFileInput(filename);
String line;
int count = 0;
while ((line = in.readLine()) != null){
count++;
}
in = new TextFileInput(filename);
arr = new Appliance[count];
while ((line = in.readLine()) != null){
count --;
arr[count] = new Appliance(line);
}
in.close();
}```
ok so
first we have something filename that has a link or holds the txt file
the TextFileInput helps looks into the whole file
its reading all of it
and now line
is gonna have a single line in the file
it reads line by line
and since its in the whileloop
it'll keep reading line by line until it reaches the end of the file
and once it does
a counter we made
will iterate to the number of lines we passed
or read
ok now
we use in again to read the whole thing?
the first loop
why are we reading the whole thing again?
counts the amount of lines
right
by iterating it
then we create the array with the length of the lines we read before
oh wait cause we reached the end right
we need to go back into the begining
yes
thats why you create a new TextFileInput
to "reset" the counter it has
right the arr = new Appliance[count]
creates a new array with the size of the lines
initializes the static variable
so we can use it
but it starts as everythin null
so we have to fill it
in the next loop
not our counter
the counter BufferedReader has
yes