 | Glasgow University Bioinformatics forum Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow |
| | |
| Author | Message |
|---|
markj
Posts: 69 Join date: 2008-09-23
 | Subject: Code for Lab 7 .. Fri Oct 03, 2008 3:36 pm | |
| the code for lab 7 and lab 7 extension exercise is a little different, So I will post them in different color (blue for lab7 and biolet for extension) import java.awt.event.ActionEvent;
import FormatIO.*;
public class Lab7 { public static void main(String[] arg){ Console con = new Console(); boolean draw = true; while(draw){ //is that better to put the question after finishing the task? con.println("Do you want to draw a box?"); String s = con.readWord(); if(s.equalsIgnoreCase("no")){ draw = false; break; //stop drawing if the answer is no //how to close a console? }
int cols = 0; int rows = 0; boolean rValid = false; boolean cValid = false;
while(!rValid){ con.println("How many row? "); rows = con.readInt(); rValid=rows>0; if(!rValid) con.println("Please input a valid number"); }
while(!cValid){ con.println("How many cols? "); cols = con.readInt(); cValid=cols>0; if(!cValid) con.println("Please input a valid number"); }
drawBox(rows,cols);
} }
private static void drawTop(Console con, int cols){ switch(cols){//just practice, not necessary to use case case 1: con.print("+"); break; case 2: con.print("++"); break; default: con.print("+"); for(int i=2;i<cols;i++) con.print("-"); con.print("+"); break; } con.println(); }
private static void drawSide(Console con, int rows, int cols){ if(rows>2){//draw side only if rows > 2 if(cols>2){ con.print("|"); for(int i=2;i<cols; i++){ con.print(" "); } con.print("|"); con.println(); } else if(cols==1){ con.print("|"); con.println(); } } }
private static void drawBox(int rows, int cols) { //draw the box in a new console Console con=new Console(); if(rows>1){ drawTop(con,cols); for(int i=2;i<rows;i++){ drawSide(con,rows,cols); } drawTop(con,cols); } else drawTop(con,cols); }
}import FormatIO.*;
public class Lab7_Extension { public static void main(String[] arg){ Console con = new Console(); boolean draw = true; while(draw){ //is that better to put the question after finishing the task? con.println("Do you want to draw a box?"); String s = con.readWord(); if(s.equalsIgnoreCase("no")){ draw = false; break; //stop drawing if the answer is no //how to close a console? }
int cols = 0; int rows = 0; boolean rValid = false; boolean cValid = false;
while(!rValid){ con.println("How many row? "); rows = con.readInt(); rValid=rows>0; if(!rValid) con.println("Please input a valid row number"); }
while(!cValid){ con.println("How many cols? "); cols = con.readInt(); cValid=cols>0; if(!cValid) con.println("Please input a valid column number"); }
drawBox(rows,cols); } } private static void drawBox(int rows, int cols){ Console con= new Console(); if(rows>1){ drawRow(con,"top",cols); for(int i=2;i<rows;i++) drawRow(con,"side",cols); drawRow(con,"top",cols); } else{ drawRow(con,"top",cols); } } private static void drawRow(Console con, String type, int num){ String sym1=""; String sym2=""; if(type.equalsIgnoreCase("top")){ sym1="+"; sym2="-"; } else{ sym1="|"; sym2=" "; } if(num>1){ con.print(sym1); for(int i=2;i<num;i++){ con.print(sym2); } con.print(sym1); con.println(); } else{ con.print(sym1); con.println(); } } } |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Code for Lab 7 .. Sat Oct 04, 2008 5:58 pm | |
| Hi wei, for me it is not clear. On the exercise paper, we have: +-----+ | | | | | | +-----+ so there are 5 horizontal marks and 3 vertical marks. However, it says this box has 5 rows and 7 columns. So how do we have to count the rows and columns??? |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Code for Lab 7 .. Sat Oct 04, 2008 6:27 pm | |
| something more, when I am putting row:1 and column:1 in your program, the output is just a "+", is it normal? |
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Code for Lab 7 .. Sat Oct 04, 2008 9:23 pm | |
| yes, I think it's normal. the exercise asked us to draw box. '+' means the corner. so if there is only 1 row and 1 column, I think the program should out put '+'. And if it has several columns but 1 row, I think the programme should out put '+--------+'. Similarly, if it has several rows but 1 column, I think our programme should output: + | | | | + in other cases if there are 5 colums and 4 rows. the programme should output: +---+ | | | | +---+ '+---+' is the first row is that clear for you? (of course, this is my own opinion) | Nico wrote: | something more,
when I am putting row:1 and column:1 in your program, the output is just a "+", is it normal? |
|
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Code for Lab 7 .. Sun Oct 05, 2008 5:37 am | |
| And of course, you could have other solutions. such as control the size of box. you could tell the user that the minimum sizi of a box should be 2*2 which is: ++ ++ | Nico wrote: | something more,
when I am putting row:1 and column:1 in your program, the output is just a "+", is it normal? |
|
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Code for Lab 7 .. Sun Oct 05, 2008 10:08 am | |
| ok thanks. But It is not very clear about how to count the rows and the columns. Everybody can have its own way of counting. What I did is: - = column and | | = row. But again maybe it is not the way Pr Cooper is doing it and I will have to change..... same for u!!! |
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Code for Lab 7 .. Sun Oct 05, 2008 10:46 am | |
| Based on what we have discussed, I think my programme might be ok. (Maybe I need to control the smallest box size). We can know the result in the next next week, any way | Nico wrote: | ok thanks.
But It is not very clear about how to count the rows and the columns. Everybody can have its own way of counting. What I did is:
- = column and | | = row.
But again maybe it is not the way Pr Cooper is doing it and I will have to change..... same for u!!! |
|
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Code for Lab 7 .. Sun Oct 05, 2008 8:17 pm | |
| yes your program is ok!!!! |
|  | | CM
Posts: 7 Join date: 2008-09-30
 | Subject: Re: Code for Lab 7 .. Mon Oct 06, 2008 6:10 pm | |
| Quickly with regards to columns and rows, what has been said before is correct. More specifically, columns is the count of vertical lines in the box and rows is the count of horizontal lines in the box. Also, does anyone know what the "happy day scenario" is? (End of Ex2...) Code for up to above point: | Code: | import FormatIO.*;
public class Ex1 {
public static void main(String[] args) { // Create new console. Console con = new Console(); // String for deciding to draw box. String drawBox = "y"; // Draw box routine while (drawBox.equalsIgnoreCase("y")) { // Get decision of whether to draw a box or not. con.println("Do you want to draw a box? (Y/N)"); drawBox = con.readLine(); // Read input. if (drawBox.equalsIgnoreCase("y")); else return; // Declare variables. int rows = 0, columns = 0; // Request size of box to be drawn. // Min 2x2. while (columns < 2 || rows < 2) { con.println("How many colums would you like?"); columns = con.readInt(); con.println("How many rows would you like?"); rows = con.readInt(); if (columns < 2 || rows < 2) { con.println("Please enter dimensions that are at least 2x2."); } } // Call drawBox drawBox(columns, rows); }
} private final static void drawTop(Console boxCon, int columns) { boxCon.print(" "); for (int i = 2; i < columns; i ) boxCon.print("-"); boxCon.print(" \n"); return; } private final static void drawSide(Console boxCon, int columns, int rows) { for (int i = 2; i < rows; i ) { boxCon.print("|"); for (int j = 2; j < columns; j ) boxCon.print(" "); boxCon.print("|\n"); } return; } private final static void drawBox(int columns, int rows) { Console boxCon = new Console(); // Call drawTop and drawSide to draw box. drawTop(boxCon, columns); if (rows > 2) drawSide(boxCon, columns, rows); drawTop(boxCon, columns); return; }
}
|
|
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Code for Lab 7 .. Mon Oct 06, 2008 7:17 pm | |
| Thanks Paul. Unfortunately, I dont know what the "happy day scenaria" is either. In addition, do you know the difference between 'break' and 'return'. (for instance, could I use break in the brown part?) | CM wrote: | Quickly with regards to columns and rows, what has been said before is correct. More specifically, columns is the count of vertical lines in the box and rows is the count of horizontal lines in the box.
Also, does anyone know what the "happy day scenario" is? (End of Ex2...)
Code for up to above point:
import FormatIO.*;
public class Ex1 {
public static void main(String[] args) { // Create new console. Console con = new Console(); // String for deciding to draw box. String drawBox = "y"; // Draw box routine while (drawBox.equalsIgnoreCase("y")) { // Get decision of whether to draw a box or not. con.println("Do you want to draw a box? (Y/N)"); drawBox = con.readLine(); // Read input. if (drawBox.equalsIgnoreCase("y")); else return; // Declare variables. int rows = 0, columns = 0; // Request size of box to be drawn. // Min 2x2. while (columns < 2 || rows < 2) { con.println("How many colums would you like?"); columns = con.readInt(); con.println("How many rows would you like?"); rows = con.readInt(); if (columns < 2 || rows < 2) { con.println("Please enter dimensions that are at least 2x2."); } } // Call drawBox drawBox(columns, rows); }
} private final static void drawTop(Console boxCon, int columns) { boxCon.print(" "); for (int i = 2; i < columns; i ) boxCon.print("-"); boxCon.print(" \n"); return; } private final static void drawSide(Console boxCon, int columns, int rows) { for (int i = 2; i < rows; i ) { boxCon.print("|"); for (int j = 2; j < columns; j ) boxCon.print(" "); boxCon.print("|\n"); } return; } private final static void drawBox(int columns, int rows) { Console boxCon = new Console(); // Call drawTop and drawSide to draw box. drawTop(boxCon, columns); if (rows > 2) drawSide(boxCon, columns, rows); drawTop(boxCon, columns); return; }
}
|
|
|  | | CM
Posts: 7 Join date: 2008-09-30
 | Subject: Re: Code for Lab 7 .. Mon Oct 06, 2008 9:41 pm | |
| 'return' I'm quite sure just takes you back out of whatever section of code you were in ad returns you to where you were in the main. When used in main it ends the programme by returning you out of the main to where there is nothing. 'break', I think under a conditional statement ('if', for example) will just take you out of the programme and end it from however many steps in you are. I thought I had tried it where you indicated and it hadn't worked. Tried it again though and seemed to work! [quote="markj"]Thanks Paul. Unfortunately, I dont know what the "happy day scenaria" is either. In addition, do you know the difference between 'break' and 'return'. (for instance, could I use break in the brown part?) [quote="CM"] // Read input. if (drawBox.equalsIgnoreCase("y")); else return; |
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Code for Lab 7 .. Wed Oct 08, 2008 12:45 pm | |
| Thanks Paul. Here is another way i thought about to draw the box. in this way, we could use only one function to draw the whole box. for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ if(i==rows-1){ if(j==cols-1) con.println("+"); else if(j==0) con.print("+"); else con.print("-"); } else if(i==0){ if(j==cols-1) con.println("+"); else if(j==0) con.print("+"); else con.print("-"); } else{ if(j==cols-1) con.println("|"); else if(j==0) con.print("|"); else con.print(" "); } } } |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Code for Lab 7 .. Wed Oct 08, 2008 8:27 pm | |
| Hi, Here are my codes, I will have a look to yours later, but I think you have already checked each others. I have done the two exercises in a quiet different way, rather than simplify the two functions. More interesting I think, it shows that switch is maybe longer, but much better for the reader. Exercise 2: import FormatIO.Console;
public class Ex2 {
public static void main(String[]args) { Console con = new Console(); int numCol = 0,numRows = 0; String answer =""; boolean repeat = true; while(repeat) { con.println("\n\n\n"); answer=""; numCol = 0; numRows = 0; while(numCol<=0) { con.println("Enter the number of col(>0): "); numCol = con.readInt(); } while(numRows<=0) { con.println("Enter the number of rows(>0): "); numRows = con.readInt(); } drawBox(numCol,numRows); while(!answer.equalsIgnoreCase("yes")&& !answer.equalsIgnoreCase("no")) { con.println("Do you want to draw another box?(yes/no): "); answer = con.readWord(); if(answer.equalsIgnoreCase("yes")) repeat = true; else repeat = false; } } } private static void drawBox(int colNB,int rowNB) { Console con2 = new Console(); drawTop(con2,colNB); drawSide(con2,rowNB,colNB); } private static void drawTop(Console con1,int colNB2) { switch(colNB2) { case 1: con1.print("+\n"); break; case 2: con1.print("++\n"); break; default: con1.print("+"); for(int i=0;i<(colNB2-2);i++) con1.print("-"); con1.print("+\n"); } } private static void drawSide(Console con1,int rowNB2,int colNB3) { switch(rowNB2) { case 1: break; case 2: drawTop(con1,colNB3); break; default: for(int i=0;i<(rowNB2-2);i++) { if(colNB3>1) { con1.print("|"); for(int j=0;j<(colNB3-2);j++) con1.print(" "); con1.print("|\n"); } else con1.print("|\n"); } drawTop(con1,colNB3); } } }
Exercise 3: import FormatIO.Console;
public class Ex3 {
public static void main(String[]args) { Console con = new Console(); int numCol = 0,numRows = 0; String answer =""; boolean repeat = true; while(repeat) { con.println("\n\n\n"); answer=""; numCol = 0; numRows = 0; while(numCol<=0) { con.println("Enter the number of col(>0): "); numCol = con.readInt(); } while(numRows<=0) { con.println("Enter the number of rows(>0): "); numRows = con.readInt(); } drawBox(numCol,numRows); while(!answer.equalsIgnoreCase("yes")&& !answer.equalsIgnoreCase("no")) { con.println("Do you want to draw another box?(yes/no): "); answer = con.readWord(); if(answer.equalsIgnoreCase("yes")) repeat = true; else repeat = false; } } } private static void drawBox(int colNB,int rowNB) { Console con2 = new Console(); drawRow(con2,colNB,rowNB); } private static void drawRow(Console con1,int colNB2,int rowNB2) { String record = ""; //for printing the bottom of the box for(int i=1;i<=colNB2;i++) { if(i==1) //if it has one col, we just print a +, the loop ends and no row is printed after (see following conditions) { con1.print("+"); record+="+"; continue; } if(i==colNB2) //if there is more than one column, the second + has to be printed { con1.print("+"); record+="+"; continue; } con1.print("-"); //will be printed only if the 2 conditions are not satisfied (because of the continue function). record+="-"; } con1.print("\n"); //the top has been printed, go to the next line if(rowNB2==2) //we do not need to check if rowNB2==1, because we do not need to print a row in this case con1.print(record); else if(rowNB2>2)//need to verify to skip the case rowNB2==1 { for(int i=0;i<(rowNB2-2);i++) { con1.print("|"); for(int j=0;j<(colNB2-2);j++) con1.print(" "); if(colNB2>1) con1.print("|"); con1.print("\n"); } con1.print(record); } } }
|
|  | | markj
Posts: 69 Join date: 2008-09-23
 | |  | | CM
Posts: 7 Join date: 2008-09-30
 | Subject: Ex3 Fri Oct 10, 2008 3:45 pm | |
| Finally got around to doing this... Did away with drawBox, didn't really see the point in having it just to call drawRow... Therefore only one helper method, drawRow. Infinite loop with a break exit for the main method, switch for down to the last row whe drawing the box. | Code: | import FormatIO.Console;
public class Ex3 {
public static void main(String[] args) {
// Declaration of variables. Console con = new Console(); int columns, rows; String drawBox;
// Loop for drawing boxes. for (;;) { // (Re)initialise drawBox to allow decision on whether to draw box. drawBox = ""; while (!drawBox.startsWith("Y") && !drawBox.startsWith("y") && !drawBox.startsWith("N") && !drawBox.startsWith("n")) { con.println("Do you want to draw a box? (Y/N)"); drawBox = con.readWord(); } // Exit if user does not wish to draw box. if (drawBox.startsWith("N") || drawBox.startsWith("n")) break; // (Re)initialise column and row variables. columns = 0; rows = 0; // Minimum size of box as 3x3, otherwise just looks wrong... while (columns < 3 || rows < 3) { con.println("How many columns should the box have?"); columns = con.readInt(); con.println("How many rows should the box have?"); rows = con.readInt(); if (columns < 3 || rows < 3) con.println("Please input a box size of at least 3x3"); } // Call drawRow to draw box. drawRow(columns, rows); }
}
private static void drawRow(int columns, int rows) {
// Create console for drawing box. Console boxCon = new Console(); // Draw box. for (int i = 1; i < rows; i++) { switch (i) { // Draw top row. case 1: boxCon.print("+"); for (int j = 2; j < columns; j++) boxCon.print("-"); boxCon.print("+\n"); break; // Draw sides. default: boxCon.print("|"); for (int j = 2; j < columns; j++) boxCon.print(" "); boxCon.print("|\n"); break; } } // Draw bottom row. boxCon.print("+"); for (int i = 2; i < columns; i++) boxCon.print("-"); boxCon.print("+\n");
}
}
|
|
|  | | |
| Page 1 of 2 | Goto page : 1, 2  |
| | Permissions of this forum: | You cannot reply to topics in this forum
| |
| |
| |
|