Last active
January 3, 2017 15:06
-
-
Save Banafasto/d3f0d20b4ed3844340ef7cd305eb73c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class Lessons6Example1 { | |
public static void main(String[] args) { | |
Date nowDate = new Date(); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(nowDate); | |
int nowYear = calendar.get(Calendar.YEAR); | |
int nowMonth = calendar.get(Calendar.MONTH); | |
int nowDay = calendar.get(Calendar.DAY_OF_MONTH); | |
Calendar lastCalendar = Calendar.getInstance(); | |
lastCalendar.set(nowYear, nowMonth - 1, nowDay); | |
long nowTime = calendar.getTimeInMillis(); | |
long lastTime = lastCalendar.getTimeInMillis(); | |
System.out.println(nowTime - lastTime); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Lessons6Example2 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
Random random = new Random(); | |
System.out.println("Input size array:"); | |
int size = scanner.nextInt(); | |
scanner.close(); | |
int[] array = new int[size]; | |
for(int i = 0;i < array.length; i += 1){ | |
array[i] = random.nextInt(10); | |
} | |
String str = arrayToString(array); | |
System.out.println(str); | |
} | |
static String arrayToString(int[] array){ | |
StringBuilder sb = new StringBuilder(""); | |
sb.append('['); | |
for(int i = 0; i < array.length; i += 1){ | |
if(i < array.length - 1){ | |
sb.append(array[i] + ", "); | |
}else{ | |
sb.append(array[i]); | |
} | |
} | |
sb.append(']'); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.util.Scanner; | |
public class Lessons6Example3 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Type a number in binary form:"); | |
String number = scanner.nextLine(); | |
scanner.close(); | |
printResult(number); | |
} | |
static boolean ifBinary(String number) { | |
boolean binary = true; | |
for (int i = 0; i < number.length(); i += 1) { | |
if (number.charAt(i) != '0' && number.charAt(i) != '1') { | |
binary = false; | |
break; | |
} | |
} | |
return binary; | |
} | |
static void printResult(String number) { | |
if (ifBinary(number)) { | |
int sum = 0; | |
for (int i = number.length() - 1, j = 0; i >= 0; i -= 1, j += 1) { | |
int x = Character.getNumericValue(number.charAt(j)); | |
int multiplication = x * (1 << i); | |
sum += multiplication; | |
} | |
System.out.println("The number in decimal format: " + sum); | |
} else { | |
System.out.println("This number is in binary format!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.util.Formatter; | |
public class Lessons6Example4 { | |
public static void main(String[] args) { | |
Formatter formatter = new Formatter(); | |
for (int i = 2; i <= 12; i += 1) { | |
formatter.format("%."+ i +"f", Math.PI); | |
String pi = formatter.toString(); | |
System.out.println(pi); | |
} | |
formatter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment