#I need help with a Project for class
1 messages · Page 2 of 1
run in debug mode
it should pause/suspend the app once execution has reached your breakpoint
wait so it's nto already in debug mode?
it was
it just copied something
you just arent using it properly
place a breakpoint
how
i thought debugging ur code was just manually finding all the stuff u were missing urself
what do u do with breakpoint
i'm gonna try deleting it
the debug thingy
and click it again
after you set a breakpoint
you run in debug mode
ok
alright so after putting the breakpoint on there, u go to window, run
and then dubug
sorry not window
just run
and then debug
thats supose to say line 13 isn't it
i clicked enable breakpoint
in debug mode there is something like step over
maybe its different on your ide
but if you click it it goes line by line
ok we just went over nodes today
no polumoriigism
just continued this today in class
right anyways
ok where do u usuualy go
i got no idea what i'm clciking
hold on i ma search it up
i'll be back
Ok lemme see if I can find something like that on eclipse
Bruh I asked the professor and he’s like we’ll learn about debugging in eclipse in the future lecture 💀
well for now lets make another loop in readfile
this is what you have ```java
import java.util.Arrays;
import javax.swing.*;
public class Project1 {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
public static void readfile (){
String filename = "Project1.txt";
TextFileInput in = new TextFileInput(filename);
String line;
int count = in.getLineCount() +1 ;
arr = new Appliance[count];
while ((line = in.readLine()) != null){
arr[count] = new Appliance(line);
count --;
}
in.close();
}
}```
Yeah I ma just figure the debug thing later
make count start in 0
int count = 0;
just after the count, and before the array, create another while loop
while ((line = in.readLine()) != null){
}```
and there you put count++;
Alright I’ll get to that
I’m about to go home rn so I’m in the bus, I’ll continue this when I’m home, I’ll be back later
u said another
so that first one stays?
alright
import java.util.Arrays;
import javax.swing.*;
public class Project1 {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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);
int count = in.getLineCount() +1 ;
arr = new Appliance[count];
while ((line = in.readLine()) != null){
arr[count] = new Appliance(line);
count --;
}
in.close();
}
}```
it should be in this order
- declare count
- the loop with count++
- in = new TextFileInput(filename); // to reset the line buffer
- declare the array with new Appliance[count];
- the loop filling the array
wait so that loop i just created should be before the other loop
import java.util.Arrays;
import javax.swing.*;
public class Project1 {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
}```
delete this: int count = in.getLineCount() +1 ;
now put the count-- before arr[count] in second loop
now it should work
lets index 20 out of bounds for lenght 20
alrigt hold on
cause i ran it without out that
lets see
oo it prints
horizontal lol
a simple /n should do
yea thats good
now we have to create the GUI
right
first though
i may be overthinking this but
how does the program know when to add a comma
This is what Arrays.toString() does
[a, b, c]
does your professor let you send many .java files?
ye he said to submit two
only two?
ye
damn
the code will look ugly but its what we have
because the textfileinput
he has it already
so we dont have to submit it
same thing with any other file he gave us
txt file that is
by the way
ye
like everything in one file?
i used to think it worked like that until a few days ago
where i discovrerd u can use code form other files
anyways
we can proceed in two ways
- create the JFrame in Main and do the stuff
- create a new class that extends JFrame
first one 💀
to main?
yes
import java.util.Arrays;
import javax.swing.*;
public class Project1 {
public static void main extends JFrame(String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
}```
uh
oh no
I forgot that your class is Project1
put the extends after Project1
not in main method
so i write extend in applaince
no, here
idk I don't use eclipse
i ma just cancel
continue
so it works
it accepted the qualitites of the Jframe object im assuming?
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
}```
now put this:
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setVisible(true);```Ask me if you have questions
ok so
main gui?
what is Main Gui
its uppercase
so maybe its not the the main method
oh right
more like Project1 gui = new Project1();
I forgot
ohhh alright
now that makes sense
cause it extends that class
alright so as for the rest
U seem to be doing that
yeah I forgot the close operation
The target screen mentioned below is a screen to which the window should be placed after the setLocationRelativeTo method is called.
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen. The center point can be obtained with the GraphicsEnvironment.getCenterPoint method.```
it puts the window on the center
yes
ok so
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}```
lets see
setTitle
i can't code this i should draw it
put gui.setDefaultCloseOperation(EXIT_ON_CLOSE); before setVisible
else it wont actually close the program if you click the x
if you don't put that the window doesn't show at all
ok size obiosuly the windth and hegiht of box
title probably shows up on the top
ok so rn
i wonder if i can use the whiteboard here
damn i can't 💀
ok i'll draw
gimme 1 minute
we probably need a JTable
yeah
thats in the main then still right
public Project1() {
}```
in Project1 class
oh the class file
like we did in Appliances before
the capital c is the class but
wait i just make another function don't i?
cause readfile
makegui
are funtions
and they are under class
that too
but with the constructor is simpler
or non-static method
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public Project1() {
}
}```
right cause Project1 is a class afterall
just like applaince
its just that it happens to contain a main method
i think
ok so this contructor is making the gui compenets
don't the gui compenets come with JFrame?
the gui components are added to JFrame
dont make your class extend JFrame
like .add()
multiple reasons why, the biggest being "too many problems occur when beginners do that"
when we can just make something do the stuff jframe has
the more powerful reason is that extends defines a relationship
an "is-a" relationship
your program is not a frame. it uses a frame
Program is a JFrame.. its not really just a frame, its an entire program
which uses a frame
i would say Program is NOT a JFrame. your class is not trying to extend the features that a frame has. it is not a frame
it uses one
other apps cannot use Program as a frame
i don't mind doing that but i think i'm supose to entend it just from this since he went over entends
though i supose it doesn't really specifiy
You can do as Dioxin says, you just have to type more
look at what it says, VS what you are doing
u mean since it says Create a GUI, we create the class for it
the entire program shouldnt exist there
That's why I would make a different class for the GUI
what they are showing is not good use of inheritance. but at least its labeled a bit better
do what you gotta do. if its a requirement, then do it
But since professor only lets OP to send two files, we would have to put another class in the same .java file
it is needed
its just a very bad way to approach it
alright
"lets use extend so we dont have to code as much"
tons of papers documenting why that is bad
but that seems to be the route your assignment wants
just dont do it on your own accord
didnt realize it was an actual requirement for your assignment
i thought we were past that phase, of misusing extends
sorry to distract from your issue
And then we wonder why entire languages discarded inheritance to prevent people from misusing it 
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public Project1() {
}
private Jtable(){
}
}```
rn, we have a panel
even James Gosling had some heat against it being introduced in Java, due to things like this
and we just need to add three different ones in there
just to separate the types and putting the elements in a JTable
create a private JTable
so uhm whats a Jtable
outside
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public Project1() {
}
private Jtable table;
}```
no
not a function
ye
private JTable table;
so just privite Jtable
ok so Jtable acts like a datatype
and table is the varible?
yes
like java private String SRN;
ok
so that table could be named tree and its fine then
ok moving on
Jtable , is that built in?
lets make a function that returns a two dimensional array
yes
oh ok so it'll make a table for us for anything
This will be the content of the table
i see
so
ok so arr could be box
we will use arr
and separate the Refrigerators and stuff
import java.util.Arrays;
import javax.swing.*;
public class Project1 extends JFrame {
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
System.out.println(Arrays.toString(arr));
}
public static Appliance [] arr;
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();
}
public static void MakeGUI() {
Project1 gui = new Project1();
gui.setTitle("Appliances");
gui.setSize(640, 480);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public Project1() {
}
private Jtable table;
static Appliance[][] getTableArray() {
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
}
}```
no, no
that thing we will make it in a function
so we don't fill the constructor
so
static Appliance[][] getTableArray() {
}```
there we will create 3 arrays with the same length as arr
Appliance[] refrigerators = new Appliance[arr.length];
oh and the other names
for every kind of appliance
after that make a for loop that iterates from zero to arr.length
when i think about it makes sense, we need an array of appliances
alright
yeah for every column
oh right
each one?
the assignment tells you to order them
yeah
both inside and with the letters i think
i forgot what selection sort did
we have the code for it he gave it to us
yeah make a function to sort arr
we will use compareTo for this
private Jtable table;
static Appliance[][] getTableArray() {
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
}
public SelectionsSort(){
}```
remember that method you added to Appliance
ye
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);
}
public boolean equals(Appliance y) {
return this.SRN.equals(y.SRN);
}
public char convert(){
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
so it comares the numbers
and returns
actaully
how do we know what it returns
if it returns the bigger one
or smller one
if you use eg e1.compareTo(e2)
it will give either a negative or positive number if they are different
and 0 if they're equal
im assuming the lowest goes on top
yeah it doesn't tell
the safest bet
cause in class
he did it that way
ok so its swap after the compareTo
public SelectionsSort(){
Appliance.compareTo
}```
ok hold on
since its in the class
we call it from applaince right
yeah im just gonna go for it
but it takes one parameter
we call it from arr[i]
its supposed to be in a loop
are we comparing one array to another?
one element of the array to another
i thought it was inside one array and sort it then move on to the next array
i misread it then
it chekcs the first letter
It's simpler to just sort the large array, and split it after in the three ones in the table array
and see's which array it goes into right?
copy the selection sort code
so after all three arrasy have the right thingys
I will tell you what to do after
okie
lemme copy paste it
private static void selectionSort(short[] array, int length) {
for ( int i = 0; i < length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < length; j++ ){
if (array[j].compareTo(array[indexLowest]) < 0)
indexLowest = j;
if ( array[indexLowest] != array[i] ) {
short temp = array[indexLowest];
array[indexLowest] = array[i];
array[i] = temp;
} // if
} // for i
} // method selectionSort
Detected code, here are some useful tools:
[WARNING] The code couldn't end properly...
Problematic source code:
private static void selectionSort(short[] array, int length) {
for ( int i = 0; i < length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < length; j++ ){
if (array[j].compareTo(array[indexLowest]) < 0)
indexLowest = j;
if ( array[indexLowest] != array[i] ) {
short temp = array[indexLowest];
array[indexLowest] = array[i];
array[i] = temp;
} // if
} // for i
} // method selectionSort```
Cause:
The code doesn't compile, there are syntax errors in this code.
## System out
[Nothing]
if (array[j] < array[indexLowest])```becomes```java
if (array[j].compareTo(array[indexLowest]) < 0)```
it looks like a basic selection sort, written using imperative code
welcome to programming
no i meant when it wasn't indented lol
ah
it looked crazy
the indentation is a personal thing
you used 2 spaces
thats the Google standard
the usual Java standard is 4 spaces. either way, it looks the same to me
if ( array[indexLowest] != array[i] )```to```java
if ( !array[indexLowest].equals(array[i]) )```
private static void selectionSort(short[] array, int length) {
for ( int i = 0; i < length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0)
indexLowest = j;
if ( !arr[indexLowest].equals(arr[i]) ) ) {
short temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = Appliance temp;
} // if
} // for i
} // method selectionSort```
this too
please learn how to use the debugger.. its built for situations like this
to pinpoint logic issues
you can solve problems like this on your own pretty easily, once you know where the issue is - debuggers do just that, help you pinpoint where the problem is
the temp variable
declare it as Appliance type
and rename array to arr
like appliance temp?
I think that's all
not just the function name
public static void SelectionsSort(){
for ( int i = 0; i < arr.length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < arr.length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0)
indexLowest = j;
if ( !arr[indexLowest].equals(arr[i]) ) ) {
short temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = temp;
} // if
} // for i
}```
good
last thing
we can delete the parameters
since arr is static
and instead of length you can use arr.length
make SelectionsSort() static
so everyone can use it
and void type
like
public static void
what about the short
ah
short temp = arr[indexLowest];
temp should be Appliance
remove the one at the top, I thought it wasnt declared
ok
public static void SelectionsSort(){
for ( int i = 0; i < arr.length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < arr.length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0) {
indexLowest = j;
}
if ( !arr[indexLowest].equals(arr[i]) ) {
short temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = temp;
} // if
} // for i
}
}```
wh is temp red
at the beginning
if its declared asa short
should be Appliance
not short
lemme remove short then and change it
the old exercise you were working with a short[] array
the Jtable is red too 💀
private Jtable table;
static Appliance[][] getTableArray() {
SelctionsSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
}```
ohh
before refrigerators
call SelectionsSort
and it will be sorted
of after
i don't think it matters
then let's make a for loop
private Jtable table;
static Appliance[][] getTableArray() {
SelectionSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];
}
public static void SelectionSort(){
for ( int i = 0; i < arr.length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < arr.length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0) {
indexLowest = j;
}
if ( !arr[indexLowest].equals(arr[i]) ) {
Appliance temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = temp;
} // if
} // for i
}
}```
yk from 0 to arr.length
yeah first forloop
we haven't learned switches
then ifs
do you have getType right?
ok but ur saying write the if to compare letters?
in Appliance
yes
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);
}
public boolean equals(Appliance y) {
return this.SRN.equals(y.SRN);
}
public char convert(){
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
getsrn
thats not converting?
i thought parse
maybe its only integer
you can name it what you want
u mean its not built in
we need that functionality that returns the first letter
so we can do like this:
if (arr[i].convert() == 'R') refrigerators[r] = arr[i];
i just relaized we have a convert method
show what you have done until now
wait these goes in above the selection sort?
or in there
in the loop we make at the bottom of the function
but you will add the counter
this
if (arr[i].convert() == 'R') {
refrigerators[r] = arr[i];
r++;
}```
and for every type
private Jtable table;
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++;
}
}
}
}
public static void SelectionSort(){
for ( int i = 0; i < arr.length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < arr.length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0) {
indexLowest = j;
}
if ( !arr[indexLowest].equals(arr[i]) ) {
Appliance temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = temp;
} // if
} // for i
}
}```
the count, its okay there?
Not in selection sort 💀
that has all the if statements?
too many fors right ☠️
yea
yes
but ye
private Jtable table;
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 SelectionSort(){
for ( int i = 0; i < arr.length - 1; i++ ) {
int indexLowest = i;
for ( int j = i + 1; j < arr.length; j++ ){
if (arr[j].compareTo(arr[indexLowest]) < 0) {
indexLowest = j;
}
if ( !arr[indexLowest].equals(arr[i]) ) {
Appliance temp = arr[indexLowest];
arr[indexLowest] = arr[i];
arr[i] = temp;
} // if
} // for i
}
}```
right this is all under the appliance srray thingy
return new Appliance[][]{refrigerators, dishwashers, microwaves};
after for loop right
ye
tell me if there's any errors
oh right lemme put that all back in ecipse
just bene editing here
ah it didn't compile
the Jtable thing is red
private Jtable table;
static Appliance[][] getTableArray() {
SelectionSort();
Appliance[] refrigerators = new Appliance[arr.length];
Appliance[] dishwashers = new Appliance[arr.length];
Appliance[] microwaves = new Appliance[arr.length];```
ye
well it printed
just not sorted
btw how do u make it print vertical
then i screenshot it
alright
just where the main is right
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
SelectionSort();
System.out.println(Arrays.toString(arr));
for (int i =0; i < arr.length; i++) {
Appliance a = arr[i];
}
}```
i have questions actaully ebfore i do that
ok
what is that for loop
no i mean there isn't any iteranting
like Lists
or stoping
there are, is just invisible
it's like a shorter way to write a for loop
so it would in reality be for(Appliance a =0;a < arr.length ;a++)?
more like: java for (int i =0; i < arr.length; i++) { Appliance a = arr[i]; }
just hidden
oh no appliance ok
wait so i do that 2 more times
how does it know which array i'm talking about though
for now we will check the big array
just arr
oh ok
idk why it took so long to register that we techincially have 4 arrays
so the big one
we just print it now
yeah run the code
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
SelectionSort();
for (int i =0; i < arr.length; i++) {
Appliance a = arr[i];
System.out.println(a);
}
}```
why you didn't use the for each 😭
anyways its the same
why did you call Arrays.toString twice?
since we are no longer printing the array horizontally, we don't need them
you can delete
we aim to help you understand. not just solve the problem you currently have, but to actually understand the code
oh right with just that, it prints horizontally
that way you wont be back with the same problems
public static void main (String[] args) {
readfile();
Arrays.toString(arr);
SelectionSort();
for (int i =0; i < arr.length; i++) {
Appliance a = arr[i];
System.out.println(a);
}
}```
now it prints like
Crystal, if theres any code you dont understand, please ask. don't just use code that you dont know
yeah thats why i've jsut been asking questions
line 3, whats that doing?
u mean the selectionsort?
Arrays.toString(arr)
it's has the array of the appliacnes
no
but when u print it
the name its clear
thats very very far
right now, its doing nothing. its dead code
so it's not being used
exactly. your IDE should be flagging you for it, with a warning
shouldn't i delete it
you should
it wont crash or anything but its a recommendation
however, Arrays.toString may be useful. you just gotta understand it properly, so you dont misuse it
thats what i mean, about understanding the code
otherwise, itll take longer to solve whatever problems you have, simply cause you arent familiar with what you got
okay
#today-i-teach message
dont just use code thats given to you
do you understand readFile()?
do you understand SelectionSort()?
everytime u call it
u put the readfile in a loop
u can make it read the whole file
line by line
right on, where is that data stored in your app?
which variable references those lines you read?
how does this data get passed to SelectionSort()?
actaully i'm not sure if it sorted
it sorted well
nemux says it works. you arent sure. thats the problem
you need to be sure. nemux cant be the only one who is sure
oh its sorting by the first letter first for the smllest
right they never said anything about numbers
ok so that means now we have to find a way to sort into arrays
based on their letter
mrm
are you sorting the lines?
whats the assignment?
strange
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);
}
public boolean equals(Appliance y) {
return this.SRN.equals(y.SRN);
}
public char convert(){
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
right on, equals is a bit weak, but that shouldn't be a big deal. you created a blueprint. do you understand everything there?
yeaah
equals actually wont work
you arent overriding it properly
put the @Override annotation on it, like you did with toString()
thats a good thing
its pointing out that you messed up
you are trying to override. but you didnt
remember, equals and toString are defined in Object
object.equals?
these are JavaDocs. a bit scary at first, but youll grow to love them
this shows the definition of equals
its equals(Object)
not equals(Application)
so yeah, your code is not overriding, when it should. thats what the squiggly was telling you
thats still an error
look at y
Object Appliance y doesnt make sense
this is what i meant by "you gotta understand the code"
but appliance is the datatype
not here
you are overriding an existing method
that method is defined in Object. all classes extend Object
and Object has no clue about Appliance type
you have to check that the object is an appliance
yup, thats one of the things they should do in equals
but they arent even there yet
if they keep making mistakes like this, itll take 2 generations to solve this relatively simple problem
imagine how long a large app would take
they gotta understand the code. these are the fundementals, the building blocks
remove Appliance
the () part is where the "parameters" are defined
know what parameters are?
ok remove appliance
but why?
but then how will it know i'm talking about it
and whats that
Application is an Object
we abstract irl all the time
baseball, soccer ball, basketball... they are all Ball
Baseball extends Ball
SoccerBall extends Ball
BasketBall extends Ball
so they are all part of the same thing
equals(Obiect) expects an Object
what does say for appliance
alright
so you can still pass Appliance objects to it
instead of equals(Appliance y)
anything that extends Object, you can pass to that method
its equals(Object y)
you have to check if y is an Appliance, so you can use SRN
instanceof is the keyword here
instance? llike instance varible?
yup
oh its staying
look at examples of equals while you're at it
y is an instance of appliance
easy info
yes
"java equals example"
wait object has to have a variable too?
what do you mean?
yup, you need to introduce a new variable
and cast the value
if using newer versions, you could define the variable within the if condition
wait so in the parenthesis
if(y instanceof Appliance app) {
// can use app
}```
object needs a letter
i'm not sure which version
right here u said casting right
so for me
(appliance) o
Appliance y = (Appliance) O;
you dont have an o in your code
i just wrote it in
oh, so you changed Object y to Object o
it was object y
and now theres an error on y
you should be checking if the parameter is an instanceof Application
yea it says the letters tharts the instance of
the parameter is Object o
not sure what you mean
i mixed up the letters
i didn't notice until u pointed them out
you should use more descriptive names for your variables
instead of y, could use obj or something similar
other, since it represents another value (something you are comparing with, checking if it equals)
instead of Appliance o, should do Appliance appliance or even Appliance app
the letter feels simple
its too simple
so simple, you got confused
the more descriptive you are, the easier things will be to navigate
if(i <= o && j > k)```
no one wants to see that
when a bug appears, no one wants to be looking through code like that
could u explain why srn would still be red
look at the variable you're using
which variable?
over here
thats the problem, dont just blindly follow..
which variable are you calling SRN from?
y
what is the type of y?
Appliance
oh of object
yup
so right there, its flagging it
Object has no SRN member
"but i did instanceof and all that jazz"
ya did it wrong
so your IDE is correct
u have app here
you arent gonna get it from Object y
thats the whole point of Appliance o
you check to see if the object is an Appliance. if it is, introduce a new variable
assign that new variable, give it the y, but turned into the proper type (casting)
Appliance o = (Appliance) y;```
y is seen as Object
thats why you need a new variable, which has the type Appliance
i got the feeling you either missed out on a lot of lessons, or your professor should be fired
no i go everyday
do you pay attention everyday? 😂
yeah of course
we haven't ever talked about this though
but i'm trying to understand
back to this
@Override
public boolean equals(Object y) {
if(y instanceof Appliance) {
Appliance o = (Appliance) y;
}
return this.SRN.equals(y.SRN);
}```
the problem is
object y right
thats what u said
but this part ```java
Appliance o = (Appliance) y;
are u saying it should be the other way around
I have class in a bit. i'll be back
no, im saying y does not have access to SRN, because the datatype is Object
you passed the value to a variable with a different datatype, Application o
that's the Application o = y; part
Ok i'm back
I'll be honest, I see what ur telling me
But i don't know what to do about it
think about the code in front of you.. look at what you have available
you have Appliance o
you arent doing anything with it
okay lemme put out what i have and think again
@Override
public boolean equals(Object obj)
) {
if(obj instanceof Appliance) {
Appliance x = (Appliance) obj;
return this.SRN.equals(other.SRN);
}```
i name it obj
for now
where did other come from?
i just wrote that as a placeholder
no need for a placeholder
use the Appliance variable
y didnt have access to SRN, because of the variable type
so other would be the same way then
what do you mean?
i thought u were saying y didn't have access
so why would putting other change anything
putting other makes no sense, thats why i asked about it
this makes no sense. take a closer look
you have a variable Object obj which you never use
you have obi instanceof
other isnt a variable. you got Application o variable which you never use
sometimes you get so focused on solving the issue, you dont realize whats in front of you
i saw you edited to fix
looks a lot better. but other, you dont know what to put
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance) {
Appliance x = (Appliance) obj;
return this.SRN.equals(x.SRN);
}```
yeah i just
you cleaned up 90% of the "what?" right there
there you go
no nvm
wdym yes
you saw it
wait ebfore
you arent done, but x.SRN was basically the golden ticket
the if maybe
then you didnt know what to put, so you put other
this
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;}
else {
return false;
}
Appliance x = (Appliance) obj;
return this.SRN.equals(x.SRN);
}```
so my x.srn
yes
obj can accept any value, because of the datatype Object
we can do appliance.equals(new Dog()); if we wanted
thats why you have the if
you wanna make sure the value isnt a dog, or a bicycle, etc.. you are checking if the value is an Appliance
does that part make sense?
but how do u check that it is
you check if obj is an instanceof Appliance
i mean other than the if statement i guess since that can't be it
which you are doing
i gues thats not enough
hm? enough for what?
thats enough to check if obj is referencing an Appliance object
and if it is
yeah but if i'm doing that
why is ther still a bug
what am i missing
because you dont understand methods properly
you are missing a ton, my friend..
but in this case, its 1 thing
you need to return
yup
ye i think i was thinking the if had a statement after the condition so it was fine
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance)
return true;
Appliance x = (Appliance) obj;
return this.SRN.equals(x.SRN);
}```
soooooo far off
look at what your code says
if(obj instanceof Appliance)
return true;```
what do you think thats gonna do?
this is why im worried about the whole "understand what the code is doing"
you were 2 inches away from the solution. but 1 change put you 5 miles away
if obj is an instance of appliance, its gonna return true
well use it
use it to compare to another serieal number
so right there, thats your goal
what if the object is not an appliance?
what then?
return false?
so you seem to understand what you need to do
now look at your code. does it reflect that?
if its not an appliance, what else can u do, u need to only use appliances
exactly
so i need a return false in there as well
if(is appliance) {
} else {
return false;
}```
right?
youd use instanceof of course, is isnt Java syntax
hold on
but thats the idea, yeah?
ye
i was about to ask
@Override
public boolean equals(Object obj){
if(obj instanceof Appliance){
return true;
}
else{
return false;
}
}```
please tell me thats at least closer
kind of. for now, remove the Appliance x = ... and the return this.SRN at the bottom
that will at least bring you to a point where the code will run (no squiggles) AND you understand what is going on
we can build on this
if its not an appliance, return false, easy
if it is, what do we do?
do we return true immediately?
what about the serial number?
yup. if its an appliance, compare
it only is saying that this varible is an applaince right nowright
so the other if was for
take this applaince
and comapre it
or uh eqauls
to another appliance
you need to check if the serials are equal, yeah
yeah
if(serials are equal) {
return true;
} else {
return false;
}```