Week 7 Exercise
Introduction
Chat-GPT is an amazing tool that being used to generate content of all types, from form letters to meeting agendas, essays and even source code. There is only one small problem with this: it doesn’t know what it’s doing.
Chat-GPT was trained on millions of lines of source code, among other things, and has seen a lot of good, well-designed code. It has also seen a lot of student code. The end result is a powerful system that can often be a great help to programmers when writing and debugging code, but will always appear confident, even when the answers it gives are wholly or partially incorrect.
Using Chat-GPT To Debug Code
In this exercise, you will use Chat-GPT to assist you in debugging some code and document your results. In the repository for the exercise, you will find a Word document with sections for each problem and operation listed below. You must put the results of the following operations into the Word document in the appropriate section.
For each problem you will do the following operations:
- Read the problem description so you know what the code should do.
- For problem 3, since we haven't covered the equals() method in lecture, you can look up the equals() method of the String class and compare it to the code shown.
- The code you are given will not be correct
- Trace it and see what you think the output will be.
- For prompt 2, trace the code once assuming "fluffy" is passed into middleChar() and once assuming "cat" is passed into middleChar().
- For prompt 3, you do not need to trace the code, but you should go line by line and try to understand what it is doing.
- Try and fix the bugs in the code yourself.
- Trace it and see what you think the output will be.
- Enter the given prompt and buggy code into Chat-GPT
- Try and rewrite the prompts to explain to Chat-GPT what your code is attempting to do, then submit the new prompt with the same buggy code.
- Did Chat-GPT perform better? Worse? The same? Explain your answers with code examples.
- Answer the following question regarding your thoughts about the output of Chat-GPT:
- Did Chat-GPT give you the correct answer? If not, what is incorrect about it’s response? Be specific, include code generated by Chat-GPT in your answer.
Problems
- Write a method countToTen that will use a for loop to count from 1 to 10, incrementing a local variable ten inside the loop, then return ten. Here is a buggy version of the method:
public int countToTen() {
int ten = 0;
for(int i = 0; i < 10; i +=2) {
++ten;
}
return ten;
}
Use the following prompt in Chat-GPT:
is there anything wrong with the following java code: [paste code here]
- Write a method called
middleCharthat will get aStringpassed in as a parameter, get the number of characters in theString, and return the middle character of theString. If theStringhas an even number of letters, it should return the first of the two middle characters (i.e. for the word fluffy, the method will return ‘u’.
public char middleChar(String s) {
int length = s.length();
int middle = length / 2;
return s.substring(0, middle);
}
Use the following prompt in Chat-GPT:
debug this java method: [paste code here]
- Write the equals() method for the
Stringclass. Below is a buggy implementation.
@Override
public boolean equals(String s) {
if(this == s) { return true; }
if(!(s instanceof String)) { return false; }
for(int i = 0; i < s.length(); ++i) {
if(this.charAt(i) != s.charAt(i)) { return false; }
}
return true;
}
Use the following prompt in Chat-GPT:
what is wrong with this java code? [paste code here]
Submission Instructions:
Make sure you are placing your responses to the prior operations in the appropriate section in the Word document within the repository. When you are done, write "DONE" in the README and then commit and push.