Posts

Showing posts from April, 2021

Creating basic tic tac toe android app using java

Image
 Marvel Tic Tac Toe Android Game ( Java ) In this blog I we will create basic android app. We will be coding in java.   package com.example.batvsiron ; import androidx.appcompat.app.AppCompatActivity ; import android.os.Bundle ; import android.util.Log ; import android.view.View ; import android.widget.Button ; import android.widget.GridLayout ; import android.widget.ImageView ; import android.widget.TextView ; import android.widget.Toast ; import java.util.Arrays ; public class MainActivity extends AppCompatActivity { int active = 0 ; public int [][] winningPosition = { { 0 , 1 , 2 } , { 3 , 4 , 5 } , { 6 , 7 , 8 } , { 0 , 3 , 6 } , { 1 , 4 , 7 } , { 2 , 5 , 8 } , { 0 , 4 , 8 } , { 2 , 4 , 6 } } ; public int [] moves = { 111 , 111 , 111 , 111 , 111 , 111 , 111 , 111 , 111 } ; public int checkWinning () { for ( int [] arr : winningPosition ) { ...

Codechef April challange : Chef and Dice solution

Image
 Solving codechef April challange :     Chef and Dice Solution in Java. Problem statement - Chef has  N N   6-sided standard dice . Each die has dimensions  1 × 1 × 1 1 × 1 × 1 . Since Chef is bored during the quarantine, he decides to stack dice for fun. First, Chef forms four vertical stacks of dice (not necessarily with the same height; empty stacks are allowed) on his table, which together make up a pile of dice with base area up to  2 × 2 2 × 2 . Among all such structures, the total visible surface area of Chef's structure must be the smallest possible. Then, Chef calculates the number of  pips  on the visible faces of all dice in the structure. A face of a die is visible if it does not touch the table or another die. Now, he is wondering: among all possible arrangements of dice, what is the maximum possible total number of visible pips? Since he is busy cooking, he ...

Codechef crypthon contest.

Image
  Problem Link Codechef crypthon contest : hard code crypt problem solution in java. import java.util.*; class cf { static void calc(int[] arr , int n) { int count = 0; for(int i=0;i<n-3;i++) { for(int j=i+1;j<n-2;j++) { int diff = arr[j] - arr[i]; for(int k=j+1;k<n-1;k++) { for(int l=k+1;l<n;l++) { if(arr[l] - arr[k] == diff && arr[k] - arr[j] == diff) { System.out.println(arr[i]+" : "+arr[j]+" : "+arr[k]+" : "+arr[l]); count ++; } } } } } System.out.println(count); } static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { int n = input.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = input.nextInt(); } calc(arr,n); ...

Round Upto 2 decimal places in java : using DecimalFormat

Image
 How to Round Number upto n decimal places in java. import java.util.*; import java.text.DecimalFormat; class MyDecimalFormater { static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { double price = 1234567890.9876; // round up to 2 decimal places // fist method System.out.println("Using Math.round() : "+Math.round(price*100.0)/100.0); // second method System.out.println("By Using DecimalFormat :"); DecimalFormat f = new DecimalFormat("###.00"); System.out.println("round upto 2 places : "+f.format(price)); // exploring DecimalFormat DecimalFormat g = new DecimalFormat("$###.000"); System.out.println("round upto 3 places and using dollar symbol : "+g.format(price)); DecimalFormat h = new DecimalFormat("\u00A5###,###.000"); System.out...

LeetCode : Reverse Words in a String III (Java Solution).

Image
 Java Solution for reverse words in a string form leetcode. Given a string  s , reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "God Ding" Output: "doG gniD" class Solution { public String reverseWords(String s) { return Arrays.stream(s.split(" ")) .map(word -> new StringBuilder(word).reverse().toString()) .collect(Collectors.joining(" ")); } } Another Solution class Solution { public String reverseWords(String s) { String[] arr = s.split(" "); String ans = ""; for(int i=0;i<arr.length;i++) ans += reverse(arr[i])+((i==arr.length-1)?"":" "); return ans; } static...

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java