What is a beginner friendly way to write this for loop? for(char i=tokens[1].cha
ID: 3798086 • Letter: W
Question
What is a beginner friendly way to write this for loop?
for(char i=tokens[1].charAt(0); i != tokens[2].charAt(0); i = (char)((tokens[1].charAt(0) > tokens[2].charAt(0))? i-1 : i+1)) {
...
}
Description Implement a class Sheet that will manage a simple spread sheet. The class will provide the following features Sheet name, numberofRo numberofColumns) ws, Maximum size is 24x26 Each element is an object: String or Integer Sheet0 untitled", 24, 26 Sheet Sheets) For the following operations, each cell is referenced by a column letter Ala..ZIz and row number 1...24. A cellRef is a string that specifies the cell as the letter column and number row. For example, cell [0][0] is "Al setCell (cellRef obj) getCell(cellRef retums the content of the cell, if fomula, returns the value of the formula getCellFomula(cellRef-retums the fomula String if the cell contains the formula, else retums null Sheet setColumnWidth width) width is to be used with toString for printing the sheet default: 8 toString 0 t the sheet as, width-8 1 item 1 2 item 2 a unit 3 item 3 11 4 total 10 10 20 30 Cell B4 contain a sum blib3, cell C10 contains a sum:a10:b10 ach cell can have a special value of the fom @SUM from:to the sum from cell from to cell to eg. SUM A1:A3 contains the sum from cells A1, A2, and A3 (a sum a10:b10- contains the sum from cells a10 andb10 Only cell with Integer will be used in these operations. The cell special value will be evaluated when the cell is accessed, behaves as Integer, or in toString0 Sheet main Test all the features of the class. Must handle exceptions.Explanation / Answer
Here, to get the first letter of a string, you must use the charAt(0). So, it is to be there in modified code also. What you can reduce is the increment/ decrement operation performed and you can move it at the end of the for loop code you have written. This leaves no change, you can write in the both ways. If you want to make for loop looking small, you can even take the initialization of i outside the loop. This is working similar as while loop if you are taking both 1 and 3 from for(1,2,3). So, to minimize this code and make it understandable, I moved the increment/decrement after the for loop implementation.
Code:
char i;
for(i = tokens[1].charAt(0); i!=tokens[2].charAt(0); )
{
//your execution code of the for loop
if(tokens[1].compareTo(tokens[2]) > 0)
{
i = (char)(i-1);
}
else
{
i = (char)(i + 1);
}
}
You can define the variable i just before the for loop, its initialization and condition checking is the same. I removed the third part from the loop and wrote it inside.
Here, the code was comparing both the strings tokens[1] and tokens[2]'s first character. If the tokens[1] is having greater character ( a<b<c..... such) then you need to decrement i or else increment it by 1 and convert it into char again. This was done using conditional operators, I changed it to simple if else condition.
Moreover, you can directly find the difference between the first two characters of the string when their first character are not same using compareTo() method of String class. So, instead of writing
i = (char)((tokens[1].charAt(0) > tokens[2].charAt(0))? i-1 : i+1)
you can write the if-else to make it simplified.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.