Posts

Showing posts from October, 2020

Enhanced Switch Statement in Java

Image
 The syntax of enhanced statement is slightly different from normal switch. Source code given below :- package com.CodeWithRK ; import java.util.Scanner ; public class DaysOfWeek { public static void main (String[] args) { Scanner scan = new Scanner(System. in ) ; System. out .print( "Enter the day number : " ) ; int dayNum = scan.nextInt() ; String day = switch (dayNum) { case 1 -> "Monday" ; case 2 -> "Tuesday" ; case 3 -> "Wednesday" ; case 4 -> "Thursday" ; case 5 -> "Friday" ; case 6 -> "Saturday" ; case 7 -> "Sunday" ; default -> "Not found due to wrong input!" ; } ; System. out .println( "The day is " +day) ; } } Note :- "break" statement is not required in this syntax. Output :-

String - ASCII Converter

 Description:- The programme changes the string from user input to list of ASCII values and vice-versa. Source Code given below:- def str_ascii (txt): asciis = [] for char in txt: asciis.append( ord (char)) return asciis def ascii_str (lst): text = "" for num in lst: text = text + chr (num) return text try : decide = int ( input ( "Press 1 to change String to ASCII. \n Press 2 to change ASCII to String : " )) if decide == 1 : usr_str = input ( "Enter your string : " ) res = str_ascii(usr_str) print ( "ASCII values : " , res) elif decide == 2 : ascii_list = [ int (num) for num in input ( "Enter ASCIIs values saperated by , : " ).split( "," )] res = ascii_str(ascii_list) print ( "String : " , res) else : print ( "Invalid Input! Try again..." ) except ValueError : print ( "Please choose a nu...

Project : The Turtle Race

Image
Source code given below :- import turtle import time import random # outcomes of a dice out = [ 1 , 2 , 3 , 4 , 5 , 6 ] # turtle screen # scr = turtle.getscreen() turtle.screensize( 200 , 400 ) # Starting line sLine = turtle.Turtle() sLine.pen( fillcolor = "white" , pencolor = "grey" , pensize = 15 ) sLine.penup() sLine.goto(- 200 , 150 ) sLine.rt( 90 ) sLine.pendown() sLine.fd( 300 ) sLine.fillcolor( "grey" ) # Finish Line sLine = turtle.Turtle() sLine.pen( fillcolor = "white" , pencolor = "grey" , pensize = 15 ) sLine.penup() sLine.goto( 300 , 150 ) sLine.rt( 90 ) sLine.pendown() sLine.fd( 300 ) sLine.fillcolor( "grey" ) # turtle object for player1 player1 = turtle.Turtle() player1.shape( "turtle" ) player1.shapesize( 2 , 2 , 1 ) player1.pen( pencolor = "black" , fillcolor = "red" ) player1.penup() player1.goto(- 200 , 100 ) player1.down() # turtle object for player1 player2 = turtle.Turtle() pl...

Project - Name Animation with Turtle

 Source Code given below - import math import time import turtle import random ############################ Functions defined here... ##################################### def write_A(pen): pen.lt( 70 ) pen.fd( 85 ) pen.rt( 130 ) pen.fd( 85 ) pen.bk( 40 ) pen.rt( 120 ) pen.fd( 35 ) def write_B(pen): pen.lt( 90 ) pen.fd( 100 ) pen.rt( 90 ) pen.circle(- 25 , 180 , steps= 10 ) pen.lt( 180 ) pen.circle(- 25 , 180 , steps= 10 ) def write_C(pen): pen.lt( 180 ) pen.circle(- 50 , 180 , steps= 10 ) def write_D(pen): pen.lt( 90 ) pen.fd( 100 ) pen.rt( 90 ) pen.circle(- 50 , 180 , steps= 10 ) def write_E(pen): pen.lt( 90 ) pen.fd( 100 ) pen.rt( 90 ) pen.fd( 50 ) pen.rt( 90 ) pen.up() pen.fd( 50 ) pen.rt( 90 ) pen.down() pen.fd( 50 ) pen.lt( 90 ) pen.fd( 50 ) pen.lt( 90 ) pen.fd( 50 ) def write_F(pen): pen.lt( 90 ) pen.fd( 100 ) pen.rt( 90 ) pen.f...