I'm practicing writing for loops in Java. This is what I have so far:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
System.out.println ("Num "+"Square "+"Num "+"Square "+"Num "+"Square "+"Num "+"Square ");
for (int x = 1; x<=20; x++) {
a = x * 1;
b = 20+a;
c = 40+a;
d = 60+a;
for (int y = 1; y <= 1; y++) {
System.out.println (a + " " + (a * a) + " " + b + " " + (b * b) + " " + c +
" " + (c * c) + " " + d + " " + (d * d));
}
}
I don't want to change the code at all (meaning I don't want to add anything more complicated than I already have), but I'd like the columns to line up. When the numbers start increasing in digits, obviously the spacing gets messed up. Any ideas on how to fix this?
EDIT: To answer your questions, I'm pretty new to this and haven't taught myself much more than this. I'm more than sure there are way better ways to do this, but I'm just practicing my for loops. Anywho, thanks for the help.
You can use tab(\t) to format it. A double tab will provide more space in your case.
// Changed Num to Number - Out of habit
System.out.println("Number\t\t" + "Square\t\t" + "Number\t\t" + "Square\t\t" + "Number\t\t" + "Square\t\t" + "Number\t\t" + "Square");
System.out.println(a + "\t\t" + (a * a) + "\t\t" + b + "\t\t" + (b * b) + "\t\t" + c + "\t\t" + (c * c) + "\t\t" + d + "\t\t" + (d * d));
public String appendBlankSpace(int val){
String returnValue=Integer.toString(val);
int lengthTobeAppend=10-returnValue.length();
//Here you can use any number instead of 10.It is for space allocated for a column
for(int i=1;i<=lengthTobeAppend;i++){
returnValue= " " + returnValue;
}
return returnValue;
}
now you can use,
System.out.println (appendBlankSpace(a) + appendBlankSpace(a * a) + appendBlankSpace(b) + appendBlankSpace(b * b) + appendBlankSpace(c) + appendBlankSpace(c * c) + appendBlankSpace(d) + appendBlankSpace(d * d));
I hope it will help you.
you can store a,b,c,d values in array like inside loop
for (int x = 1; x<=20; x++) {
a = x * 1;
b = 20+a;
c = 40+a;
d = 60+a;
int [] array = {a,b,c,d}
//using enhanced for loop
for(int ele : array){
System.out.println(ele +"\t\t"+ ele*ele)
}
}
output will appear like :
2 4
3 9
4 16
Use printf.
public class Squarely {
public static void main(String[] args) {
for (int x = 1; x <= 20; ++x) {
int a = x, b = 20+x, c = 40+a, d = 60+a;
System.out.printf("%6d %6d %6d %6d %6d %6d %6d %6d%n",
a, a*a, b, b*b, c, c*c, d, d*d);
}
}
}
Related
I want to split any number to any identical pieces and the last remaining but not dividable piece will be the last piece. I wrote this code but I know that it should be more simple way to do this :) For example; 7500 divided by 2000 and the last modulus part will be the last part. Any suggestions?
public class MyClass {
public static void main(String args[]) {
int x =7500;
int y = 2000;
int lastPartCount = 0;
String result = new String();
if(x%y != 0){
lastPartCount = x%y;
}
int newx = x-lastPartCount;
for(int i=1; i<=(newx/y); i++){
if(i == 1){//first member
result = "part " + i + ": 0-" + y*i;
}else
{
result = "part " + i + ": " + (y*(i-1)) + "-" + y*i;
}
System.out.println(result);
if(i == (newx/y)){//last member
result = "part " + (i+1) + ": " + (y*(i)) + "-" + x;
System.out.println(result);
}
}
}
}
the result is like this:
part 1: 0-2000
part 2: 2000-4000
part 3: 4000-6000
part 4: 6000-7500
You can simplify your code like the following:
public static void main(String args[]) {
int x = 7500;
int y = 2000;
for (int i = 0; i < x/y; i++) {
System.out.println("Part " + (i+1) + ": " + y*i + " - " + y*(i+1));
}
if (x%y != 0) {
System.out.println("Part " + ((x/y)+1) + ": " + (x/y)*y + " - " + x);
}
}
(x/y)*y) is not equal to x since you divide integers, so (x/y)*y is actually the same as the "next" i of the for-loop.
You can also try the below code:
private void test() {
int x = 7500;
int y = 2000;
int j = 0;
int newX = x;
while (newX > y) {
System.out.println("Part " + (j + 1) + " = " + y * j++ + " - " + y * j);
newX -= y;
}
System.out.println("Part " + (j + 1) + " = " + j * y + " - " + x);
}
Alternative approach using two variables in your for loop and Math.min():
int x = 7500;
int y = 2000;
for (int i = 0, p = 1; i < x; i += y, p++) {
System.out.printf("Part %d: %d - %d%n", p, i, Math.min(i+y,x));
}
I want to write a program such that if the input is true, it adds a and b, and if the input is false, it subtracts b from a. Also, when it is an ArrayList, if the input is true, it picks the maximum value, and if the input is false, it picks the minimum value.
public class Source7_3 {
public static void main(String[] args) {
OverLoading mm = new OverLoading();
int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int[] a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a and b are part of the class, it would not be necessary to pass them
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int[] a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String[] args) {
OverLoading mm = new OverLoading();
int[] a = new int[10];
System.out.println("\n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("\n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}
I have 3 integer as input: a, b and c.
I'm trying to create a function to calculate the number progress at which a match will occur.
I used the following code to output the number and then the combination of input numbers. I'm trying to create a function where I can input a, b, and, c and it will return the progress for which x==a,y==bandz==c`.
int progress = 0;
for (int x = 0; x <= 160; x++) {
for (int y = 0; y <= 160; y++) {
for (int z = 0; z <= 160; z++) {
progress++;
System.out.println(progress + " " + x + " " + y + " " + z);
}
}
}
Your formula is:
progress = 1 + z_iterations
progress = 1 + z + 161*y_iterations
progress = 1 + z + 161*( y + 161*x_iterations )
progress = 1 + z + 161*( y + 161*x )
Final form:
progress = 1 + z + 161*y + 25921*x
You method should be:
public int computeProgress (int a, int b, int c) {
return 1 + c + 161*b + 25921*a;
}
I need to make a two dimensional table that has two columns with the following code.
public static void printCommonLogTable() {
double x = 0.0;
int i = 1;
while (x <= 10.0) {
System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5; }
System.out.println("");
}
public static void main(String[] args) {
printCommonLogTable();
}
The first column in the table should be the number for which you are computing the log and the second column should be the result.
But when I run this, everything is on the same line.
That's because your call to System.out.println(""); is misplaced: it should be at the end of the while loop (but inside it). This is easier to see when the code is correctly indented, like this:
public static void printCommonLogTable() {
double x = 0.0;
while (x <= 10.0) {
System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5;
System.out.println();
}
}
Note that I removed the unused i variable, and replaced System.out.println(""); with just System.out.println();
You can also combine the two print statements in one:
public static void printCommonLogTable() {
double x = 0.0;
while (x <= 10.0) {
System.out.println(x + " " + Math.log(x) + " ");
x = x + 0.5;
}
}
public static void main(String args[]) {
double x = 0.0;
int i = 1;
while (x <= 10.0) {
System.out.println(x + " " + Math.log(x) + " "); // old : System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5; }
}
The println("...") method prints the string "..." and moves the cursor to a new line. The print("...") method instead prints just the string "...", but does not move the cursor to a new line. Hence, subsequent printing instructions will print on the same line. The println() method can also be used without parameters, to position the cursor on the next line.
I have a question on the method and process of how to look at these generated arrays.
Basically I want to create an array of [a,b,c,(a+b+c)] and as well as a second array of [d,e,f,(d+e+f)] and if the third element in array1 and array2 are the same, display the arrays to strings.
int num = 10;
for(int a = 0; a < num; a++){
for(int b = 0; b < num; b++){
for(int c = 0; c < num; c++){
if(a<=b && b<=c){
arrayOne[0] = a;
arrayOne[1] = b;
arrayOne[2] = c;
arrayOne[3] = (a+b+c);
}
}
}
}
for(int d = 0; d < num; e++){
for(int e = 0; e < num; e++){
for(int f = 0; f < num; f++){
if(d<=e && e<=f){
arrayTwo[0] = d;
arrayTwo[1] = e;
arrayTwo[2] = f;
arrayTwo[3] = (f -(d+e));
}
}
}
}
as you can see I am beyond stump.I am not quite sure where i can get each iteration of the arrays and compare the values by matching the sums in each array and as well as displaying the respective array they are in. Thank you all in advanced.
If I understand your question correctly if a=1, b=3, c=4 and d=2, e=3, f=3 you'd like to print something along the lines of 1 + 3 + 4 = 8 = 2 + 3 + 3. First, what you're doing right now is creating two arrays like Floris described in the comment. What you want to do is store all the values in one array of arrays, as follows:
int max; \\ To determine the value of max see the edit below.
int array[][] = new int[max][num];
int index = 0;
for (int a=0; a < num; a++) {
for (int b=a; b < num; b++) {
for (int c=b; c < num; c++) {
array[index][0] = a;
array[index][1] = b;
array[index][2] = c;
array[index][3] = a + b + c;
index++;
}
}
}
for (int i = 0; i < max; i++) {
for (int j = i; j < max; j++) {
if (array[i][3] == array[j][3]) {
string outString = array[i][0] + " + " + array[i][1] + " + " + array[i][2] + " = " + array[i][3] + " = " + array[j][0] + " + " + array[j][1] + " + " + array[i][2];
System.out.println(outString);
}
}
}
You can see that I improved performance by starting b from a and c from b since you are throw out all the values where b < a or c < b. This also should eliminate the need for your if statement (I say should only because I haven't tested this). I needed to use an independent index due to the complexities of the triple nested loop.
Edit 2: Ignore me. I did the combinatorics wrong. Let An,k be the number of unordered sets of length k having elements in [n] (this will achieve what you desire). Then An,k = An-1,k + An,k-1. We know that An,1 = n (since the values are 0, 1, 2, 3, 4, ..., n), and A1,n = 1 (since the only value can be 11111...1 n times). In this case we are interested in n= num and k = 3 , so plugging in the values we get
A_num,3 = A_num-1,3 + A_num,2
Apply the equation recursively until you come to an answer. For example, if num is 5:
A_5,3 = A_4,3 + A_5,2
= A_3,3 + A_4,2 + A_4,2 + A_5,1
= A_3,3 + 2(A_4,2) + 5
= A_2,3 + A_3,2 + 2(A_3,2) + 2(A_4,1) + 5
= A_2,3 + 3(A_3,2) + 2(4) + 5
= A_1,3 + A_2,2 + 3(A_2,2) + 3(A_3,1) + 2(4) + 5
= 1 + 4(A_2,2) + 3(3) + 2(4) + 5
= 1 + 4(A_1,2) + 4(A_2,1) + 3(3) + 2(4) + 5
= 1 + 4(1) + 4(2) + 3(3) + 2(4) + 5
= 5(1) + 4(2) + 3(3) + 2(4) + 5
It looks like this may simplify to (num + (num - 1)(2) + (num - 2)(3) + ... + (2)(num - 1) + num) which is binomial(num, num) but I haven't done the work to say for sure.
int givenNumber = 10;
int []arrayOne = new int [4];
int []arrayTwo = new int [4];
int count = 0;
for ( int i = 0; i < givenNumber; i ++)
{
for ( int x = 0; x < givenNumber; x ++ )
{
for ( int a = 0; a < givenNumber; a++ ){
arrayOne[0] = (int)(a * java.lang.Math.random() + x);
arrayOne[1] = (int)(a * java.lang.Math.random() + x);
arrayOne[2] = (int)(a * java.lang.Math.random() + x);
arrayOne[3] = (int)(arrayOne[0]+arrayOne[1]+arrayOne[2]);
}
for ( int b = 0; b < givenNumber; b++ ){
arrayTwo[0] = (int)(b * java.lang.Math.random() + x);
arrayTwo[1] = (int)(b * java.lang.Math.random() + x);
arrayTwo[2] = (int)(b * java.lang.Math.random() + x);
arrayTwo[3] = (int)(arrayTwo[0]+arrayTwo[1]+arrayTwo[2]);
}
if (arrayOne[3] == arrayTwo[3])
{
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayOne[a] + " + ");
} System.out.print(arrayOne[2] + " = " + arrayOne[3] + " = ");
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayTwo[a] + " + ");
} System.out.print(arrayTwo[2]);
System.out.println("\n");
count += 1;
}
}
}
if (count == 0)
System.out.println(
"\nOops! you dont have a match...\n" +
"Please try running the program again.\n");