Skip to content

Instantly share code, notes, and snippets.

@Parassharmaa
Last active February 13, 2018 11:22
Show Gist options
  • Select an option

  • Save Parassharmaa/0d4f61993acaeb099b39058f6dc0e3e3 to your computer and use it in GitHub Desktop.

Select an option

Save Parassharmaa/0d4f61993acaeb099b39058f6dc0e3e3 to your computer and use it in GitHub Desktop.
Practice: Java Programs
public class BowTie {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
for(int i=0; i<2*n+1; i++) {
for(int j=0; j<2*n+1; j++) {
if ((j>i && j<2*n-i) || (j>(2*n-i) && j<i && i>n))
System.out.print(" . ");
else
System.out.print(" * ");
}
System.out.println();
}
}
}
/*
$ javac BowTie.java && java BowTie 3
* . . . . . *
* * . . . * *
* * * . * * *
* * * * * * *
* * * . * * *
* * . . . * *
* . . . . . *
*/
public class Ex {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
for(int i=0; i<2*n+1; i++) {
for(int j=0; j<2*n+1; j++) {
if (i==j || j==2*n-i )
System.out.print(" * ");
else
System.out.print(" . ");
}
System.out.println();
}
}
}
/*
$ javac Ex.java && java Ex 3
* . . . . . *
. * . . . * .
. . * . * . .
. . . * . . .
. . * . * . .
. * . . . * .
* . . . . . *
*/
public class NumPattern {
public static void main(String[] args) {
for(int i =1; i<8; i++) {
for (int j=1; j<14; j++) {
if (j<=i)
System.out.print(j);
else if (j>i && j<2*i)
System.out.print(2*i-j);
else
System.out.print(" ");
}
System.out.println();
}
}
}
/*
$ javac NumPattern.java && java NumPattern 6
1
121
12321
1234321
123454321
12345654321
1234567654321
*/
public class Pay {
int hours;
float base;
Pay(int hours, float base) {
this.hours = hours;
this.base = base;
}
void get_payment() {
if(this.hours > 60) {
System.out.print("Error: Overtime");
return;
}
if(this.base < 8.0) {
System.out.print("Error: Less Base wage");
return;
}
if(this.hours > 40)
System.out.println(this.base * 40 + (this.base * 1.5 * (this.hours%40)));
else
System.out.println(this.base * this.hours);
}
public static void main(String args[]) {
Pay e1 = new Pay(40, 10);
e1.get_payment();
}
}
/*
$ javac Pay.java && java Pay
400.0
*/
public class Triangle {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if (j<i)
System.out.print(" . ");
else
System.out.print(" * ");
}
System.out.println();
}
}
}
/*
$ javac Triangle.java && java Triangle 6
* * * * * *
. * * * * *
. . * * * *
. . . * * *
. . . . * *
. . . . . *
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment