Last active
January 3, 2017 15:29
-
-
Save Banafasto/8c8df0b9fbb4031643c9a55468c3bf86 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.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Scanner; | |
public class Lessons6Example1Lavel2 { | |
public static void main(String[] args) { | |
Date nowDate = new Date(); | |
Date yourDate = new Date(); | |
SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy"); | |
SimpleDateFormat sdfNew = getNewFormater(getNowCalendar(nowDate, sdf), getYourCalendar(yourDate, sdf)); | |
String result = sdfNew.format(yourDate); | |
System.out.println("Result date: " + result); | |
} | |
static Calendar getNowCalendar(Date nowDate, SimpleDateFormat sdf){ | |
String dateText = sdf.format(nowDate); | |
System.out.println("Now: " + dateText); | |
Calendar calendarNow = Calendar.getInstance(); | |
calendarNow.setTime(nowDate); | |
return calendarNow; | |
} | |
static Calendar getYourCalendar(Date yourDate, SimpleDateFormat sdf){ | |
System.out.println("Input date (format dd MM yyyy)"); | |
Scanner scanner = new Scanner(System.in); | |
String yourDateText = scanner.nextLine(); | |
scanner.close(); | |
try{ | |
yourDate = sdf.parse(yourDateText); | |
} catch(ParseException e){ | |
System.out.println(e); | |
} | |
yourDateText = sdf.format(yourDate); | |
System.out.println("Your date: " + yourDateText); | |
Calendar calendarYour = Calendar.getInstance(); | |
calendarYour.setTime(yourDate); | |
return calendarYour; | |
} | |
static SimpleDateFormat getNewFormater(Calendar calendarNow, Calendar calendarYour){ | |
StringBuilder sb = new StringBuilder(""); | |
if(calendarNow.get(Calendar.DAY_OF_MONTH) == calendarYour.get(Calendar.DAY_OF_MONTH)){ | |
sb.append("dd "); | |
} | |
if(calendarNow.get(Calendar.MONTH) == calendarYour.get(Calendar.MONTH)){ | |
sb.append("MM "); | |
} | |
if(calendarNow.get(Calendar.YEAR) == calendarYour.get(Calendar.YEAR)){ | |
sb.append("yyyy"); | |
} | |
SimpleDateFormat sdfNew = new SimpleDateFormat(sb.toString()); | |
return sdfNew; | |
} | |
} |
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 Lessons6Example2Lavel2a { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Input first number: "); | |
int one = scanner.nextInt(); | |
String oneString = Integer.toBinaryString(one); | |
System.out.println("The first in a binary system: " + oneString); | |
System.out.print("Input second number: "); | |
int two = scanner.nextInt(); | |
String twoString = Integer.toBinaryString(two); | |
System.out.println("The second in a binary system: " + twoString); | |
scanner.close(); | |
printResult(one, two); | |
} | |
static void printResult(int one, int two){ | |
int xor = one ^ two; | |
String xorString = Integer.toBinaryString(xor); | |
int distanceHamming = 0; | |
for(int i = 0; i < xorString.length(); i += 1){ | |
if(xorString.charAt(i) == '1'){ | |
distanceHamming += 1; | |
} | |
} | |
System.out.println("Hamming distance: " + distanceHamming); | |
} | |
} |
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 Lessons6Example3Lavel2 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Input number:"); | |
int number = scanner.nextInt(); | |
scanner.close(); | |
String numberString = Integer.toString(number); | |
for (int i = 1; i < numberString.length() / 2; i += 1) { | |
if (numberString.length() % i == 0) { | |
boolean yes = true; | |
for (int j = 0; j < numberString.length() - i; j += i) { | |
if (numberString.charAt(j) != numberString.charAt(j + i)) { | |
yes = false; | |
break; | |
} | |
} | |
if (yes) { | |
for (int k = 0; k < i; k += 1) { | |
System.out.print(numberString.charAt(k)); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment