I have created a mitsubishi logo builder as shown below. It takes input from the logo.txt file and uses those ints/chars to create however many logos that are identified in the logo.txt file. I just feel that my code is very repetitive and I would like to clean it up in terms of cutting it into separate methods instead of repeating over and over, are there any reccomemndations on how to clean this up? I would appreciate any and all feedback. Or even just a starting point. Thank you in advance.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
input = new Scanner(new FileInputStream("config.txt"));
//initialize ithe variable's input and output
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
int size = Integer.parseInt(inputStream.nextLine());
int numLogos = Integer.parseInt(inputStream.nextLine());
char letter1 = inputStream.next().charAt(0);
char letter2 = inputStream.next().charAt(0);
for(int bee = numLogos; bee > 0; bee--){
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
System.out.println();
}
}
}
}
You use this many times in the code:
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
It can be replaced by
for(int x = 0; x < 2*size; x++) {
System.out.print(letter2);
}
Also, this:
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
Can be replaced by
for(int x = 0; x < 2*(size - y); x++) {
System.out.print(letter1);
}
Related
So I get the error in the findSmallest and findLargest methods, but I understand the error, I just don't understand why it is happening. I am comparing 2 doubles in a 2D array, but it is still telling me I can't compare them with a '>'. Any help would be greatly appreciated. You can skip over most of the main method to the places where I have the problems.
import java.util.Scanner;
public class DivingScores
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] judge = new String[7]; //
System.out.println("Enter judges' names:"); //
for(int x = 0; x<judge.length; x++) //
judge[x] = input.nextLine(); //
//
String[] diver = new String[4]; //
System.out.println("Enter divers' names:"); //
for(int x = 0; x<diver.length; x++) //
diver[x] = input.nextLine(); // Creating and Instantiating
//
double[] diveDifficulty = new double[7]; //
for(int x = 0; x<diveDifficulty.length; x++) //
diveDifficulty[x] = input.nextDouble(); //
//
double[][] diverScore = new double[4][7]; //
for(int x = 0; x<diverScore.length; x++) //
for(int y = 0; y<diverScore[x].length; y++) //
diverScore[x][y] = input.nextDouble(); //
System.out.println("Judges"); //
for(int x = 0; x<judge.length; x++) //
System.out.printf("%15s, ", judge[x]); //
System.out.println(); //
//
System.out.println("Divers"); //
for(int x = 0; x<diver.length; x++) //
System.out.printf("%13s, ",diver[x]); //
System.out.println(); //
//
System.out.println("Dive Difficulty"); // Printing
for(int x = 0; x<diveDifficulty.length; x++) //
System.out.printf("%2.2f, ", diveDifficulty[x]); //
System.out.println(); //
//
System.out.println("Diver Scores"); //
for(int x = 0; x<diverScore.length; x++) //
{ //
System.out.println(diver[x]); //
for(int y = 0; y<diverScore[x].length; y++) //
System.out.printf("%2.2f ", diverScore[x][y]); //
System.out.println(); //
}
findSmallest(diverScore);
findLargest(diverScore);
awardMedal(calcScore(diverScore, diveDifficulty, diver.length), diver);
}
public static void findSmallest(double[][] diverScore)
{
for(int x = 0; x<diverScore.length; x++)
{
int smallest = 0;
for(int y = 0; y<diverScore[x].length; y++)
{
if(y > 0 && diverScore[smallest] > diverScore[y]) //Error Here
smallest=y;
}
diverScore[x][smallest] = 0;
}
}
public static void findLargest(double[][] diverScore)
{
for(int x = 0; x<diverScore.length; x++)
{
int largest = 0;
for(int y = 0; y<diverScore[x].length; y++)
{
if(y > 0 && diverScore[largest] < diverScore[y]) //Error Here
largest=y;
}
diverScore[x][largest] = 0;
}
}
public static double[] calcScore(double[][] diverScore, double[] diveDifficulty, int divers)
{
double[] scores = new double[divers];
for(int x=0; x<divers; x++)
{
double total = 0;
for(int y=0;y<diverScore[x].length;y++)
total += diverScore[x][y]*diveDifficulty[y];
scores[x]=total;
}
return scores;
}
public static void awardMedal(double[] scores, String[] diver)
{
int first = 0;
int second = 0;
int third = 0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[first]<scores[x])
first = x;
}
System.out.println(diver[first] + scores[first] + " Gold");
scores[first]=0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[second]<scores[x])
second = x;
}
System.out.println(diver[second] + scores[second] + " Silver");
scores[second]=0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[third]<scores[x])
third = x;
}
System.out.println(diver[third] + scores[third] + " Bronze");
}
}
I have no idea why the error is occurring in this case but is there an alternative way to do what I have done?
it is a 2D array, so when you are doing
diverScore[smallest] > diverScore[y]
you are comparing Arrays not doubles.
double[][] diverScore
As per your input
for(int x = 0; x<diverScore.length; x++) //
for(int y = 0; y<diverScore[x].length; y++) //
// here
You need to do an inner loop
I was wondering how to make this opencv c++ code in Java
uchar *ptr = eye.ptr<uchar>(y);
I have been looking around and I think I can use the uchar as a byte... but I have no idea what the code to get the .ptr in java
Heres my code so far
private Rect getEyeball(Mat eye, MatOfRect circles) {
int[] sums = new int[circles.toArray().length];
for (int y = 0; y < eye.rows(); y++) {
// OpenCV method uchar *ptr = eye.ptr<uchar>(y); Goes here
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.toArray().length; i++) {
if (sums[i] < smallestSum) {
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles.toArray()[smallestSumIndex];
}
The full C++ code is
cv::Vec3f getEyeball(cv::Mat &eye, std::vector<cv::Vec3f> &circles)
{
std::vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
int radius = (int)std::round(circles[i][2]);
if (std::pow(x - center.x, 2) + std::pow(y - center.y, 2) < std::pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.size(); i++)
{
if (sums[i] < smallestSum)
{
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles[smallestSumIndex];
}
Distilling down your C++:
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
// A loop not using ptr.
++ptr;
}
}
You're simply getting the pixel value at (x,y) from eye.
So, just use one of the overloads of Mat.get.
int[] values = new int[eye.channels()];
for (int y = 0; y < eye.rows(); y++) {
for (int x = 0; x < eye.cols(); x++) {
eye.get(x, y, values);
int value = values[0];
// A loop not using ptr.
}
}
Note that using get(int, int, int[]) rather than get(int, int) here means that you avoid allocating a new array for each iteration, which will make things a heck of a lot faster.
I have a 2d array called tiles[x][y] which goes till 9 so has 100 things inside of it.
How can I get another array and put everything from the 2d array into the normal array?
int counter = 0;
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tiles[y][x] = new loopVak(Color.WHITE, x*tileWidth, y*tileHeight);
}
}
This is how the 2d array is made, mapwidth and mapheight is 10.
If you want to convert tiles to a new 1D Array then you can simply do something like this:
int k = 0, newArray[] = new loopVak[100];
for(int i = 0; i < mapWidth; i++) {
for(int j = 0; j < mapHeight; j++) {
newArray[k++] = tiles[i][j];
}
}
If you do not want the 2D array in the first place then you can do something like this:
int counter = 0, newArray[] = new loopVak[100];
for(int x = 0; x < mapWidth; x++) {
for(int y = 0; y < mapHeight; y++) {
newArray[counter++] = new loopVak(Color.WHITE, x * tileWidth, y * tileHeight);
}
}
I have an array that I'm trying to blur using a kernel, but the loop doesn't finish for some reason, here's the code:
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
nlm2[x][y] += 100 * (int) ((float) nlm[x][y]*(float)kernel[(kx+3)*(ky+3)-1]);
System.out.println(x+" "+y);
System.out.println(kx+" NLM: "+(float) nlm[x][y]);
System.out.println(ky+" Kernel: "+(float)kernel[(kx+3)*(ky+3)-1]);
}
}
}
}
It seems to stop after
x = 0; y = 6
kx = ky = -2
There are no errors in the console, and it shows another print screen after this little loop-de-loop.
This lines might be the problem.-
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
You're incrementing x and y respectively, instead of kx and ky.
I'm guessing you really meant.-
for (int kx = -2; kx <= 6; kx ++) {
for (int ky = -2; ky <= 6; ky ++) {
So basically I am trying to fill the array[] element with 9 values, however I am not sure how to get the index within this code. I want it to fill the next element in the array each time.
public boolean check(int data[][], int x, int y){
int array[] = new int[9];
int xmax = x+3;
int ymax = y+3;
for(int i = x; i<xmax; i++){
for(int j = y; j<ymax; j++){
array[] = data[i][j];//array[what here?]
}
}
}
for (int i = x; i < xmax; ++i) {
for (int j = y; j < ymax; ++j) {
array[3 * (i - x) + (j - y)] = data[i][j];
}
}