CMPS 200 Homework Assignment 8 with Solution, Fall 2012
American University of Beirut · Introduction to Programming · 9 pages · with solution
More CMPS 200 files
- Homework 11 · Fall 2018HomeworkView
- Homework Last Assignment · Fall 2018HomeworkView
- Homework Asst10 · Fall 2017HomeworkView
- Homework 9 · Spring 2017HomeworkView
- Homework Assignment 2 · Fall 2016HomeworkView
- Homework Assignment 5 · Fall 2016HomeworkView
- Homework Assignment 6 · Fall 2016HomeworkView
- Homework 1 · Spring 2016HomeworkView
Show the text of this homework
import java.awt.Color;
/*Ghassan Fakher
* section 6
* gkf01
* 201301739*/
public class GrayImage {
public static void main(String[] args)
{
Picture original = new Picture("monkey.jpg");
//we don't want to
change the original picture, so we make a copy of it
Picture pic = new Picture(original);
int width = pic.width();
int height = pic.height();
for(int row = 1; row < height; row++)
{
for(int column = 1; column < width; column++)
{
Color pixel = pic.get(row, column);
int r = pixel.getRed();
int g = pixel.getGreen();
int b = pixel.getBlue();
int average = (int)((r + b + g) / 3);
Color shadeGray = new Color (average, average, average);
//a gray color has same arguments; it is on the interior diagonal of the
RBG cube..
pic.set(row, column, shadeGray);
pic.show();
}
}
}
}
import java.awt.Color;
/*ghassan fakher
gkf01
section 6
201301739*/
public class PulseHeart {
public static void main(String[] args)
{
double size = 5;
pulseHeart(size);
}
public static void heart(double size, Color c)
{
double x0 = -50; double y0 = -50;
double x1= 50; double y1 = 50;
StdDraw.setXscale(x0, x1);
StdDraw.setYscale(y0, y1);
double[] absicca = {0,-size,0,size};
double[] ordinate = {-size,0,size,0};
double distance = Math.sqrt(Math.pow(size, 2) + Math.pow(size, 2));
StdDraw.setPenColor(c);
StdDraw.filledPolygon(absicca, ordinate);
StdDraw.filledCircle(-size/2, size/2, distance/2);
StdDraw.filledCircle(size/2, size/2, distance/2);
}
public static void pulseHeart(double size)
{
while(true)
{
int dr = 0; int db = 0; int dg = 0; //db, dg, and dr represent
the change in each component of the color
Color heartColor = new Color(0, 200, 0);
double newSize = size;
for(int i = 0; i < 10; i++)
{
heartColor = new Color(dr, dg, 255 - db);
StdDraw.setPenColor(heartColor);
heart(newSize, heartColor);
StdDraw.show(100);
newSize += 3;
dr += 25; db += 25;
StdDraw.show(100);
}
StdDraw.clear();
}
}
}
/*Ghassan Fakher
* gkf01
* section 6
* 201301739*/
public class RemoveDuplicates {
public static void main(String[] args)
{
String fileOne = args[0];
String fileTwo = args[1];
String fileThree = args[2];
workFiles(fileOne, fileTwo, fileThree);
}
//method that puts the content of each file inside an array
public static void workFiles(String fileOne, String fileTwo, String
fileThree)
{
In file1 = new In(fileOne);
//starts with file one
int numInts1 = file1.readInt();
int[] firstFile = new int[numInts1];
for(int i = 0; i < firstFile.length; i++)
firstFile[i] = file1.readInt();
In file2 = new In(fileTwo);
int numInts2 = file2.readInt();
int[] secondFile = new int[numInts2];
for(int i = 0; i < secondFile.length; i++)
secondFile[i] = file2.readInt();
removeDuplicants(fileThree,firstFile, secondFile);
}
//method that writes to a third file the union of the first and second
files witout any duplicants
public static void removeDuplicants(String fileThree,int[] firstFile,
int[] secondFile)
{
Out file3 = new Out (fileThree);
int length1 = firstFile.length;
int length2 = secondFile.length;
String union = "";
for(int i = 0; i < length1; i++)
union += firstFile[i];
for(int i = 0; i < length2; i++)
union+= secondFile[i];
String noDup = "";
for(int i = 0; i < union.length(); i++)
{
if(!noDup.contains("" + union.charAt(i)))
noDup += union.charAt(i);
}
file3.print(noDup);
}
}
/*ghassan fakher
gkf01
section 6
*/
public class Similar {
public static void main(String[] args)
{
boolean similar=false;
In file1= new In(args[0]);
In file2= new In(args[1]);
String firstFile=file1.readAll().toLowerCase();
String secondFile = file2.readAll().toLowerCase();
if(firstFile.equals(secondFile))
similar = true;
System.out.print("Are the two files similar? " + similar);
}
}
import java.awt.Color;
/*ghassan fakher
* section 6
* gkf01
* 201301739*/
public class Smooth
{
public static void main(String[]args)
{
Picture pic = new Picture(args[0]);
for(int row =1 ; row<pic.width() -1 ; row++)
{
for(int column = 1 ; column < pic.height()-1 ; column++)
{
Color color1 = pic.get(row+1,column);
Color color2 = pic.get(row,column+1);
Color color3 = pic.get(row-1,column);
Color color4 = pic.get(row-1,column-1);
int red = ((color1.getRed() + color2.getRed() +
color3.getRed() + color4.getRed())/4);
int blue = ((color1.getBlue() + color2.getBlue() +
color3.getBlue() + color4.getBlue())/4);
int green =((color1.getGreen() + color2.getGreen() +
color3.getGreen() + color4.getGreen())/4);
Color newColor = new Color(red,green,blue);
pic.set(row,column,newColor);
}
}
pic.show();
}
}
import java.awt.Color;
/*Ghassan Fakher
* section 6
* 201301739
* gkf01*/
public class TicTacToe {
public static void main(String[] args)
{
int[] cells = new int[9];
double random = Math.random();
if(random >= 0.5)
{
while(!checkWin(cells, 1) && !checkWin(cells, 0))
{
userTurn(cells);
drawGrid(cells);
if(checkWin(cells, 0))
{
StdOut.println("Congratulations. You Win!");
break;
}
computerTurn(cells);
drawGrid(cells);
if(checkWin(cells, 1))
{
StdOut.println("Computer Wins!");
break;
}
}
}
else { // computer starts first
while(!checkWin(cells, 1) && !checkWin(cells, 0))
{
computerTurn(cells);
drawGrid(cells);
if(checkWin(cells, 1))
{
StdOut.println("Computer Wins!");
break;
}
userTurn(cells);
drawGrid(cells);
if(checkWin(cells, 0))
{
StdOut.println("Congratulations. You Win!");
break;
}
}
}// end of else statement
}// end of startGame method
public static boolean userTurn(int[] cells)
{
int id = 0;
StdOut.print("Enter a cell: ");
int userChoice = StdIn.readInt();
if(cells[userChoice] == 0)
cells[userChoice] = 1;
else
{
while(cells[userChoice] != 0)
{
StdOut.println("Already selected\nEnter a cell:");
userChoice = StdIn.readInt();
}
cells[userChoice] = 1;
}
drawGrid(cells);
return checkWin(cells, id);
}
public static boolean computerTurn(int[] cells)
{
int id = 1;
int random = (int)(Math.random() * 8) + 1;
while(cells[random] != 0)
random = (int)(Math.random() * 8) + 1;
cells[random] = 2;
StdOut.println("The computer chooses " + random);
drawGrid(cells);
return checkWin(cells, id);
//checks whether the computer wins
}
public static boolean checkWin(int[] cells, int id)
{
boolean userWins=false;
boolean computerWins=false;
if (id==0){
if(cells[0]==1 && cells[1]==1 &&cells[2]==1)
{
userWins=true;
} else if(cells[3]==1 && cells[4]==1 &&cells[5]==1)
{
userWins=true;
} else if(cells[6]==1 && cells[7]==1 &&cells[8]==1)
{
userWins=true;
} else if(cells[0]==1 && cells[3]==1 &&cells[6]==1)
{
userWins=true;
}else if(cells[1]==1 && cells[4]==1 && cells[7]==1)
{
userWins=true;
}else if(cells[2]==1 && cells[5]==1 &&cells[8]==1)
{
userWins=true;
} else if(cells[0]==1 && cells[4]==1 && cells[8]==1)
{
userWins=true;
}else if(cells[2]==1 && cells[4]==1 && cells[6]==1)
{
userWins=true;
}
}else if(id==1)
{
if(cells[0]==2 && cells[1]==2 &&cells[2]==2)
{
computerWins=true;
} else if(cells[3]==2 && cells[4]==2 &&cells[5]==2)
{
computerWins=true;
} else if(cells[6]==2 && cells[7]==2 &&cells[8]==2)
{
computerWins=true;
} else if(cells[0]==2 && cells[3]==2 &&cells[6]==2)
{
computerWins=true;
}else if(cells[1]==2 && cells[4]==2 && cells[7]==2)
{
computerWins=true;
}else if(cells[2]==2 && cells[5]==2 &&cells[8]==2)
{
computerWins=true;
} else if(cells[0]==2 && cells[4]==2 && cells[8]==2)
{
computerWins=true;
}else if(cells[2]==2 && cells[4]==2 && cells[6]==2)
{
computerWins=true;
}
}
if(id==1)
return computerWins;
else
return userWins;
}
public static void drawGrid(int[] cells)
{
StdDraw.setXscale(0, 300);
StdDraw.setYscale(0, 300);
StdDraw.setPenColor(Color.black);
StdDraw.square(150., 150., Math.sqrt(180000) / 2);
for(int row = 50 ; row <= 250 ; row += 100)
{
for(int column = 50; column <= 250; column+= 100)
{
StdDraw.square(row * 1.0, column * 1.0 ,
Math.sqrt(10000) / 2.0);
}
}
for(int y = 50 ; y <= 250 ; y += 100)
{
for(int x = 50; x <= 250; x+= 100)
{
for(int i = 0; i < cells.length; i++)
{
if(i == 0)
{x = 50; y = 50;}
else if(i == 1)
{x = 150; y = 50;}
else if(i == 2)
{x = 250; y = 50;}
else if(i == 3)
{x = 50; y = 150;}
else if(i == 4)
{x = 150; y = 150;}
else if(i == 5)
{x = 250; y = 150;}
else if(i == 6)
{x = 50 ; y = 250;}
else if(i == 7)
{x = 150 ; y = 250;}
else if(i == 8)
{x = 250 ; y = 250;}
if(cells[i] == 1)
{
StdDraw.line(x - 50, y - 50, x + 50, y +
50);
StdDraw.line(x - 50, y + 50, x + 50, y -
50);
}
else if(cells[i] == 2)
{
StdDraw.filledCircle(x, y, 40.);
}
}
}
}
}
}