Glasgow University Bioinformatics forum

Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow
 
HomeHome  ­FAQFAQ  ­SearchSearch  ­MemberlistMemberlist  ­UsergroupsUsergroups  ­RegisterRegister  ­Log inLog in  
Share | 
 

 Code for Lab 7 ..

View previous topic View next topic Go down 
Goto page : Previous  1, 2
AuthorMessage
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Code for Lab 7 ..   Fri Oct 10, 2008 5:07 pm

You are right. Only one method(using nest loop) is enough for this exercise.
I think the reason why they asked us to use two or three different help method is to let us get familiar with the help method. Your program is quite clear. Just it seems you have control the input of rows and cols in your main method. So it seems you do not neet to consider it again in your help method.


CM wrote:
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.

Back to top Go down
View user profile
CM



Posts: 7
Join date: 2008-09-30

PostSubject: Re: Code for Lab 7 ..   Sat Oct 11, 2008 8:32 am

Yeah.
main
takes the inputs and then passes only the two numerical variables for the height and width of the box to drawRow which does the rest before going back to main to see if they want to do it again.
Back to top Go down
View user profile
Mike



Posts: 25
Join date: 2008-09-18

PostSubject: Re: Code for Lab 7 ..   Sun Oct 12, 2008 2:00 pm

About "Happy day" scenario. This term was in the lecture 4 or 5. I think, that by this term is meant that interaction between program and user is passing in a normal way, i.e. user immediately inputs correct numbers.
Other cases (opposite to Happy day) - when user inputs e.g. -5 columns or 0 rows. (IMHO)

Code:
import FormatIO.*;

public class Ex3
{
   public static void main(String[] args)
   {
      // creating helper objects
      Console con = new Console();
      boolean wantToDraw = true;
      int columnNumber = 0;
      int rowNumber = 0;
      
      
      // offer to play
      while (wantToDraw)
         {
            con.print("Do you want to draw a box? (Yes/No): ");
            String answer = con.readWord();
            wantToDraw = answer.equalsIgnoreCase("yes");
            if (!wantToDraw)
               break; // exit in the case of refuse
      
            // checking number of columns and rows
            boolean validColumn = false;
            while (!validColumn)
               {
                  con.println("How many columns?");
                  columnNumber = con.readInt();
                  if (columnNumber <= 0)
                     con.print("Give a number > 0\n");
                  validColumn = (columnNumber > 0);
               }
            
            boolean validRow = false;
            while(!validRow)
               {
                  con.println("How many rows?");
                  rowNumber = con.readInt();
                  if (rowNumber <= 0)
                     con.print("Give a number > 0\n");
                  validRow = (rowNumber > 0);
               }
            drawBox(columnNumber, rowNumber); // calling a helper method drawBox
         }
   }
   
   // the first helper method drawBox
   private static void drawBox(int columnNumber, int rowNumber)
      {
         Console drawCon = new Console("DrawCon"); // creating console for drawings
         drawRow(drawCon, columnNumber, rowNumber); // calling a helper method drawCon
      }
      
      // the second helper method drawCon
      private static void drawRow(Console drawCon, int columnNumber, int rowNumber)
         {
            for (int k = 0; k <= 1; k++)
               {
                  if (columnNumber == 1)
                     drawCon.print("+\n");
                  else
                     drawCon.print("+");
                  for (int i = 3; i <= columnNumber; i++)
                     drawCon.print("-");
                  if (!(columnNumber == 1))
                     drawCon.print("+\n");
            
                  if (k == 0)
                     {
                        for (int m = 3; m <= rowNumber; m++)
                        if (rowNumber > 2)
                           {
                              if (columnNumber == 1)
                                 drawCon.print("|\n");
                              else
                                 drawCon.print("|");
                              for (int j = 3; j <=columnNumber; j++)
                              drawCon.print(" ");
                              if (columnNumber != 1)
                                 drawCon.print("|\n");
                           }
                     }
                  if (rowNumber == 1)
                     k++;
               }
         }
}
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Code for Lab 7 ..   Sun Oct 12, 2008 11:17 pm

Thanks mike.
Your program is quite smart I should say, though it is not easy for me to understand it at first.
I think I should change my program to simplify the drawBox method just like you did.


Mike wrote:
About "Happy day" scenario. This term was in the lecture 4 or 5. I think, that by this term is meant that interaction between program and user is passing in a normal way, i.e. user immediately inputs correct numbers.
Other cases (opposite to Happy day) - when user inputs e.g. -5 columns or 0 rows. (IMHO)
private static void drawRow(Console drawCon, int columnNumber, int rowNumber)
{
for (int k = 0; k <= 1; k++)
{
if (columnNumber == 1)
drawCon.print("+\n");
else
drawCon.print("+");
for (int i = 3; i <= columnNumber; i++)
drawCon.print("-");
if (!(columnNumber == 1))
drawCon.print("+\n");

if (k == 0)
{
for (int m = 3; m <= rowNumber; m++)
if (rowNumber > 2)
{
if (columnNumber == 1)
drawCon.print("|\n");
else
drawCon.print("|");
for (int j = 3; j <=columnNumber; j++)
drawCon.print(" ");
if (columnNumber != 1)
drawCon.print("|\n");
}
}
if (rowNumber == 1)
k++;
}
}
}
[/code]
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: Code for Lab 7 ..   Mon Oct 13, 2008 12:03 pm

Hey mike, your code is really good. If I may, but it is really to "titillate" (I am not sure I can use that word in english, whatever), you are using an "else" just to handle the case you have to go to the next line "\n", maybe you can find another way of doing it and save some code lines....

But you're code is really good, it is just a suggestion.

Mike wrote:

for (int k = 0; k <= 1; k++)
{
if (columnNumber == 1)
drawCon.print("+\n");
else
drawCon.print("+");

for (int i = 3; i <= columnNumber; i++)
drawCon.print("-");
if (!(columnNumber == 1))
drawCon.print("+\n");

Back to top Go down
View user profile
seni



Posts: 6
Join date: 2008-09-18

PostSubject: lab7 exercise   Wed Oct 15, 2008 6:24 pm

hey guys to tell u the truth i think as each day goes by this java is getting more and more complicated . Do anyone ve any thing that i can study to make life in programming easy for me.
Anyway after spending like 4 hrs in the lab i was able to scrabble something on lab7 and its extensions.here my code

but pls if u ve any material for me t o study pls help me out do not forget i really need help.
import FormatIO.*;
import java.util.*;
lab7
public class EX1 {

private static void drawTop(Console con,int nCols)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");


else
con.print("-");

} con.println();
}
private static void drawSide(Console con,int nCows,int nRows)
{
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCows;i++)
{
if (i==1||i==nCows)
con.print("|");
else
con.print(" ");

} con.println();
}
}
public static void main(String [] args)
{
Console con = new Console();
//int i = con.readInt();
drawTop(con,Cool;
drawSide(con,8,7);
drawTop(con,Cool;
}

}


lab7 Ex2
mport FormatIO.*;
import java.util.*;
public class Ex2 {


private static void drawTop(Console con,int nCols)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");


else
con.print("-");

} con.println();
}
private static void drawSide(Console con,int nCows,int nRows)
{
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCows;i++)
{
if (i==1||i==nCows)
con.print("|");
else
con.print(" ");

} con.println();
}
}

private static void drawBox(int cols, int rows){
Console con = new Console();
//int i = con.readInt();
drawTop(con,cols);
drawSide(con,cols,rows);
drawTop(con,cols);
}

public static void main(String [] args)
{
boolean valid = false;
String response;
while (!valid){


for(;Wink{

Console con=new Console();
con.println("how many rows");
int nRows=con.readInt();
con.println("how many cols");
int nCols=con.readInt();
drawBox(nCols,nRows);

con.print("Do u want to draw box again? ");
response = con.readWord();
if(response.equalsIgnoreCase("NO")){
valid = true;
break;
}
}
}
}
}


lab7 Ex3

import FormatIO.*;
import java.util.*;
public class ex3 {

private static void drawBox(int nCols, int nRows){
Console con = new Console();
drawRow(con,nCols,nRows);
}

private static void drawRow(Console con,int nCols,int nRows)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");
else
con.print("-");
} con.println();
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("|");
else
con.print(" ");

}
con.println();
}
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");
else
con.print("-");
}
}

public static void main(String [] args)
{
boolean valid = false;
String response;
while (!valid){


for(;Wink{

Console con=new Console();
con.println("how many rows");
int nRows=con.readInt();
con.println("how many cols");
int nCols=con.readInt();
drawBox(nCols,nRows);

con.print("Do u want to draw box again? ");
response = con.readWord();
if(response.equalsIgnoreCase("NO")){
valid = true;
break;
}

}
}
}




}
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: Code for Lab 7 ..   Thu Oct 16, 2008 5:34 pm

I have found this:

http://www.freejavaguide.com/corejava.htm

Look for some tutorials on the web, you can find plenty of them.
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Code for Lab 7 ..   Fri Oct 17, 2008 2:07 pm

you can download the powerpoint slides for our text book 'BIG JAVA'. I think that would be help

seni wrote:
hey guys to tell u the truth i think as each day goes by this java is getting more and more complicated . Do anyone ve any thing that i can study to make life in programming easy for me.
Anyway after spending like 4 hrs in the lab i was able to scrabble something on lab7 and its extensions.here my code

but pls if u ve any material for me t o study pls help me out do not forget i really need help.
import FormatIO.*;
import java.util.*;
lab7
public class EX1 {

private static void drawTop(Console con,int nCols)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");


else
con.print("-");

} con.println();
}
private static void drawSide(Console con,int nCows,int nRows)
{
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCows;i++)
{
if (i==1||i==nCows)
con.print("|");
else
con.print(" ");

} con.println();
}
}
public static void main(String [] args)
{
Console con = new Console();
//int i = con.readInt();
drawTop(con,Cool;
drawSide(con,8,7);
drawTop(con,Cool;
}

}


lab7 Ex2
mport FormatIO.*;
import java.util.*;
public class Ex2 {


private static void drawTop(Console con,int nCols)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");


else
con.print("-");

} con.println();
}
private static void drawSide(Console con,int nCows,int nRows)
{
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCows;i++)
{
if (i==1||i==nCows)
con.print("|");
else
con.print(" ");

} con.println();
}
}

private static void drawBox(int cols, int rows){
Console con = new Console();
//int i = con.readInt();
drawTop(con,cols);
drawSide(con,cols,rows);
drawTop(con,cols);
}

public static void main(String [] args)
{
boolean valid = false;
String response;
while (!valid){


for(;Wink{

Console con=new Console();
con.println("how many rows");
int nRows=con.readInt();
con.println("how many cols");
int nCols=con.readInt();
drawBox(nCols,nRows);

con.print("Do u want to draw box again? ");
response = con.readWord();
if(response.equalsIgnoreCase("NO")){
valid = true;
break;
}
}
}
}
}


lab7 Ex3

import FormatIO.*;
import java.util.*;
public class ex3 {

private static void drawBox(int nCols, int nRows){
Console con = new Console();
drawRow(con,nCols,nRows);
}

private static void drawRow(Console con,int nCols,int nRows)
{
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");
else
con.print("-");
} con.println();
for(int j=2;j<nRows;j++){
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("|");
else
con.print(" ");

}
con.println();
}
for (int i=1;i<=nCols;i++)
{
if (i==1||i==nCols)
con.print("+");
else
con.print("-");
}
}

public static void main(String [] args)
{
boolean valid = false;
String response;
while (!valid){


for(;Wink{

Console con=new Console();
con.println("how many rows");
int nRows=con.readInt();
con.println("how many cols");
int nCols=con.readInt();
drawBox(nCols,nRows);

con.print("Do u want to draw box again? ");
response = con.readWord();
if(response.equalsIgnoreCase("NO")){
valid = true;
break;
}

}
}
}




}
Back to top Go down
View user profile
 

Code for Lab 7 ..

View previous topic View next topic Back to top 
Page 2 of 2Goto page : Previous  1, 2

Permissions of this forum:You cannot reply to topics in this forum
Glasgow University Bioinformatics forum :: IT Strand :: Programming-