#Need Help with a project for class
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
I'm working on 9 files for this so this is what i have in them
public class Project2 {
public static void main(String args[]) {
}
}```
public class ApplianceGUI {
}```
Detected code, here are some useful tools:
public class ApplianceGUI {
}
abstract public class Appliance {
protected int price;
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
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:
System out
[Nothing]
public class Refrigerator extends Appliance {
private int feet;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
Detected code, here are some useful tools:
public class Microwave extends Appliance {
private int watts;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
Detected code, here are some useful tools:
public class Dishwasher extends Appliance{
private boolean uci;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}
Detected code, here are some useful tools:
public class ApplianceNode {
protected Appliance data;
protected ApplianceNode next;
public ApplianceNode(Appliance d) {
data = d;
next = null;
}
}```
Detected code, here are some useful tools:
System out
[Nothing]
public abstract class ApplianceList {
protected ApplianceNode first;
protected ApplianceNode last;
protected int length;
public ApplianceList() {
ApplianceNode dummy= new ApplianceNode(null);
first = dummy;
last = dummy;
length = 0;
}
public void append(Appliance a) {
ApplianceNode x = new ApplianceNode(a);
last.next = x;
last = x;
length++;
}
}```
Detected code, here are some useful tools:
public class SortedApplianceList extends ApplianceList {
public SortedApplianceList() {
super();
}
public void add(Appliance a) {
ApplianceNode p = first;
if (first == last) {
append(a);
}
while (p != null) {
if (p.next == null) {
append(a);
break;
}
if(p.next.data.compareTo(a)>= 0) {
ApplianceNode y = new ApplianceNode(a);
y.next = p.next;
p.next = y;
length++;
break;
}
p = p.next;
}
}
}````
Detected code, here are some useful tools:
What's your specific question?
I need help with it step by step because there is alot I don't understand
But what specifically are you stuck on?
so I guess starting with the applainces
i'm stuck on the appliances rn
my refeg, dishwah, and micro
But what about of Appliances are you stuck on.
all extend my applaince class
well for one the appliances
public class Refrigerator extends Appliance {
private int feet;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
i'm not sure how to write it
or how to use the tostring method here
i put price in my applance class
since it was supose to be in all the other 3 applainces
but as for the tostring method, i know we overrride it for the use of each appliance
but idk what to do with it
abstract public class Appliance {
protected int price;
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
So each appliance has a toString which should return something else.
And your assignment just states what's unique per appliance type.
yea
i guess the first letter
i should probably mention this project is built off our first one
but in that one, we only had two files
like this was how it was written
I uploaded your attachments as Gist.
and this was the appliance class
public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
but with this project, we're using lists and nodes and inheriterence
maybe it doesn't help saying that but i thought i'd mention it
but yeah starting with the appliances in this project, since each has to return its own type of appliance back
Please do try to ask specific questions. Because I am honestly a bit puzzled what your specific issue is.
You keep stating appliances, without stating what the issue is.
i'm not sure how to go about it
Alright, I need help with how to make it return an appliance for each type of appliance
what i mean is
public class Refrigerator extends Appliance {
private int feet;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
public class Microwave extends Appliance {
private int watts;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
public class Dishwasher extends Appliance{
private boolean uci;
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
these all extend my appliance class
i'm trying to figure out how to make it return that string of an applaince back
You just call the toString on appliance. It's generic, so the one of the actual type will be called.
u say its generic, so that means my code doesn't know which appliance its talking about right?
maybe i need constuctors
but my appliance class has them
Appliance fridge = new Refridgerator();
When you then call toString on fridge the Refridgerator toString is used.
public class Refrigerator extends Appliance {
private int feet;
public Refrigerator(int f){
feet = f;
}
@Override
public String toString() { // just returns the serial number
Appliance fridge = new Refrigerator();
return fridge;
}
}```
it probably doesn't go in the method but i need to understand
so i wanna ask things
so since refeg extended to appliance
it means i can decalre refeg the same way i can declare an applaince?
Refridgerator should have a constructor which accepts feet.
Or call a setter.
As it stands you never get it.
Also a toString should not construct a new object.
It should just return a textual representation.
alright but where would declaring the appliance go if no in the method or constuctor
Wherever you need it. Most likely where you start processing the file.
I recommend checking out:
oh our main method
MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.
- The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
The end of the course is marked by creating your own Asteroids game clone! - The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
- To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.
The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ.
- TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
- VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
- IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
IntelliJ requires installing a version no newer than2023.1- because the IntelliJ TMC Plugin doesn't work with newer installs.
The IntelliJ Community version is free and all you need to install the TMC plugin.
This server currently recommends IntelliJ.
To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.
Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)
About the course - Java Programming
so this would be after reading the file i'm assuming
right i guess it woukdn't make sense to create the datatype of the refergrertor within it's class
I'll look at this, I thinking rn I need to make the arraylists in my main method for these three
Also apologies but I have to go rn but I'll be back in a couple of hours
No problem.
@fluid ibex
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 👍
Ok I'm back
I think I'm confused
my subclasses wouldn't need to have the tostring method if it's already in the appliance class right?
abstract public class Appliance {
protected double price;
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
public class Refrigerator extends Appliance {
private int feet;
public Refrigerator(String SRN){
super(SRN);
}
@Override
public String toString(SRN) { // just returns the serial number
return SRN;
}
}```
public class Microwave extends Appliance {
private int watts;
public Microwave(String SRN) {
super(SRN);
}
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
public class Dishwasher extends Appliance{
private boolean uci;
public Dishwasher(String SRN) {
super(SRN);
}
@Override
public String toString() { // just returns the serial number
return SRN;
}
}```
they would not, correct
so do i just make the tostrign method in appliance abstract
or well this is how i have rn
abstract public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
public class Refrigerator extends Appliance {
private int feet;
private double price;
public Refrigerator(String SRN, double p,int f){
super(SRN);
feet = f;
price = p;
}
@Override
public String toString() { // just returns the serial number
return "Refrigerator - Serial Number: " + getSRN();
}
}```
public class Microwave extends Appliance {
private int watts;
private double price;
public Microwave(String SRN, double p,int w) {
super(SRN);
watts=w;
price =p;
}
@Override
public String toString() {
return "Microwave - Serial Number: " + getSRN();
}
}```
public class Dishwasher extends Appliance{
private boolean underCountInstall;
private double price;
public Dishwasher(String SRN,double p, boolean uci ) {
super(SRN);
price =p;
underCountInstall = uci;
}
@Override
public String toString() {
return "Dishwasher - Serial Number: " + getSRN();
}
}```
also why does it tell me my getSRN is undefined when i extended to appliance
@fluid ibex
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 👍
@fluid ibex ping me in a few hours, i'll loop back. doing work
Alright got it
hey sorry I was caught up with work for alot longer than i hoped
i wanted to say i got my code to work
but i still wanted to help to understand everything
I'll just post the code here but rn I just want explnations to what some parts of the code is doing
public class Project2 {
public static void main(String args[]) {
ApplianceGUI allAppliance = new ApplianceGUI();
allAppliance.readfile();
allAppliance.DisplayGUI();
}
}```
abstract public class Appliance {
private String SRN;
public Appliance (String SRN) {
this.SRN = SRN;
}
@Override
public String toString() { // just returns the serial number
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); // check if two serial numbers equal each other
}
public char convert(){ // takes in a serial number and returns the first letter
if ((SRN.isEmpty())) return 'X';
return SRN.charAt(0);
}
public String getSRN() {
return SRN;
}
public void setSRN(String SRN) {
this.SRN = SRN;
}
}```
public class Refrigerator extends Appliance {
private int feet;
private double price;
public Refrigerator(String SRN, double p,int f){
super(SRN);
feet = f;
price = p;
}
@Override
public String toString() { // just returns the serial number
return "Refrigerator - Serial Number: " + getSRN();
}
}```
public class Microwave extends Appliance {
private int watts;
private double price;
public Microwave(String SRN, double p,int w) {
super(SRN);
watts=w;
price =p;
}
@Override
public String toString() {
return "Microwave - Serial Number: " + getSRN();
}
}```
public class Dishwasher extends Appliance{
private boolean underCountInstall;
private double price;
public Dishwasher(String SRN,double p, boolean uci ) {
super(SRN);
price =p;
underCountInstall = uci;
}
@Override
public String toString() {
return "Dishwasher - Serial Number: " + getSRN();
}
}```
public class ApplianceNode {
protected Appliance data;
protected ApplianceNode next;
public ApplianceNode(Appliance d) {
data = d;
next = null;
}
}```
public abstract class ApplianceList {
protected ApplianceNode first;
protected ApplianceNode last;
protected int length;
public ApplianceList() {
ApplianceNode dummy= new ApplianceNode(null);
first = dummy;
last = dummy;
length = 0;
}
public void append(Appliance a) {
ApplianceNode x = new ApplianceNode(a);
last.next = x;
last = x;
length++;
}
}```
public class SortedApplianceList extends ApplianceList {
public SortedApplianceList() {
super();
}
public void add(Appliance a) {
ApplianceNode p = first;
if (first == last) {
append(a);
}
while (p != null) {
if (p.next == null) {
append(a);
break;
}
if(p.next.data.compareTo(a)>= 0) {
ApplianceNode y = new ApplianceNode(a);
y.next = p.next;
p.next = y;
length++;
break;
}
p = p.next;
}
}
}```
this was the file we had to read from
R45839RHJ783,2195,22
R389502JNM32,972,16
R2847786SFLK,1599,20
M1189SSK9023,295,50
M8579UKH9RI3,49,10
M7389FIE9283,99,25
D78WJH7662DJ,589,Y
D44NDMDOE758,399,N
D82974IOUFDB,1280,Y
R37859KDIUW7,1285,17
R378OEJF7834,1366,15
M349FJKEI374,299,30
D234NDJF8372,458,Y
M8934KFD8394,129,12
M4589EFKEJ34,159,14
R349FJKE9383,875,12
D73I9ER83742,952,Y
D28994KFH893,457,N
R38DH39462KE,2399,25
R34RE837563I,456,10
and finally this is what it prints
now for my questions
for my sortedApplianceList, could u explain what's fully going on in the add method with the conditions that lead to appending, the one where we check if the node is null and the compareTo for clarification
for this code,
public void FillGUI() {
Container myContentPane = getContentPane();
TextArea refeg = new TextArea();
TextArea dishWash = new TextArea();
TextArea micro = new TextArea();
setLayout(new GridLayout(1,3));
ApplianceNode curr = rList.first.next;
while(curr !=null) {
refeg.append(curr.data.toString() + "\n");
curr=curr.next;
}
ApplianceNode curr2 = dList.first.next;
while(curr2 !=null) {
dishWash.append(curr2.data.toString()+ "\n");
curr2=curr2.next;
}
ApplianceNode curr3 = mList.first.next;
while(curr3 !=null) {
micro.append(curr3.data.toString() + "\n");
curr3=curr3.next;
}
refeg.setEditable(false);
dishWash.setEditable(false);
micro.setEditable(false);
myContentPane.add(refeg);
myContentPane.add(dishWash);
myContentPane.add(micro);
}```
could you explain what exactly ```ApplianceNode curr = rList.first.next;``` is saying?
waking up
public class SortedApplianceList extends ApplianceList {
public SortedApplianceList() {
super();
}
public void add(Appliance a) {
ApplianceNode p = first;
if (first == last) {
append(a);
}
while (p != null) {
if (p.next == null) {
append(a);
break;
}
if(p.next.data.compareTo(a)>= 0) {
ApplianceNode y = new ApplianceNode(a);
y.next = p.next;
p.next = y;
length++;
break;
}
p = p.next;
}
}
}
okay so I think that if (first == last) is a ghetto "is empty" check
then the next part is an insertion sort
just puts the element in the right place in the list
but didn't you write this code?
what exactly am i explaining?
No I did but I also had help so there were some things that weren’t fully clear
The conditions as to when we append
was that help AI
I just wanted an explanation on what they did to make sure I got it fully
No a tutor
I don’t use ai to do code
well basically it always appends
But to explain things to me sometimes
so long as the item isn't already in the list
while (p != null) {
if (p.next == null) {
append(a);
break;
}
if(p.next.data.compareTo(a)>= 0) {
ApplianceNode y = new ApplianceNode(a);
y.next = p.next;
p.next = y;
length++;
break;
}
p = p.next;
}
But it would defeat the purpose of me learning if I did that 😭
correct, good job
but okay
the way this works is - if we reach the end of this linked list we append
we only reach the end if we didn't find a place to put it before the end
so going left to right
lets say we have this list
4 -> 7 -> 9 -> 22
and we want to insert 8
so we start here
V
4 -> 7 -> 9 -> 22
if(p.next.data.compareTo(a)>= 0) {
so the next is 7
is 7 greater than or equal to 8? no
so next
V
4 -> 7 -> 9 -> 22
the next one is 9
so is 9 greater than or equal to 8? yes
so this is where we splict in 8
4 -> 7 -> 8 -> 9 -> 22
now lets insert 9999
it will make it all the way to the end
V
4 -> 7 -> 8 -> 9 -> 22 -> null
and this is where it hits this branch
if (p.next == null) {
append(a);
break;
}
and thats when we append
when there is no place before the end to add the thing
Alright so for this one, could u explain ApplianceNode curr=rList.first.next;
Like what exactly it’s doing
What do you think it does?
Going through the linked list
But there is a lot of dots
Actually it’s the while u would use to go through each of list until the end
Do you know the difference between: foo.bla and foo.bla()?
But I know once u do, u add in the info u collected from reading onto that specific appliance list
If foo.bla is a built in function then no
But if it’s about the parenthesis
What is foo?
No idea
Ok, what is your rList?
Haven’t ever heard of foo
Don't think too far.
It’s meant to be a sorted list of refrigerators
Yes that’s what’s it’s declared as
It’s a linked list
That wasn't the question.
rList is the variable that stores a linked list data type I made
That's not the question.
My question is literally:
foo; <= variable or method
bar(); <= variable or method
Because the reason you don't get what's going on is seemingly you not noticing/knowing/recognizing what they respectively are.
Oh I see
Second one is method
Cause parenthesis
First one would be a vairbke
Every method has parenthesis
That was the question then
So what would rList.first indicate?
Its a pointer maybe
What?
Yes, and do you know how to call the methods of a variable, or access it's accessible variables?
Ok. so first has no parenthesis, so what is it? Variable or method?
I only think it’s a method cause of the dot
But it doesn’t have parenthesis
So it could just be a vairbke
No, a dot just indicates you're acting upon what's to the left of the dot.
Okay so it would be good to ask
What does rList.first indicate
Cause I’m seeing that I don’t know
I am trying to figure out where/why you're stuck.
You know rList is a variable, and you know its type.
You know . indicates we're using something of the variable/class to the left of it.
I think I was confused with nodes
Cause I recently learned it
But first is the first node
In this case the specific type doesn't even matter.
Does that mean it’s the first thing in the linked list
It's just .first vs .first(). Both could indicate a note. In the former case as a variable, the second case a method.
Well second since they also use .next.
oh alright
okay so rList.first
means it's looking at the first node?
and then .next
Yes.
the one after?
It gets the next element of the first node.
Or rather, it gets the next element of the node it's called upon. Which in this case is the first node.
so the first calls the next node then
u know what
lemme draw this out
i need visual represenation
Ok so currently that’s what just that line is doing right
Okay
This is what I think is happening
So there is a node called curr and I’m saying that inside the list, it’ll be pointed to the first node which points the next node
And the while loop is gonna go through the entire list until it’s very last which is null
And throughout that process, each time, it’s gonna print the current serial number in the list in the textarea
And once it’s printed that current one, the next current node will be the node after that in the rList
That’s what I think is happening
I’m sure something is wrong with that but that’s what I think is going on
@fluid ibex
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 👍