Posts

Showing posts from December, 2019

he challenge here is to read n lines of input until you reach EOF, then number and print all n lines of content. Hint: Java's Scanner.hasNext() method is helpful for this problem.

CSZONE Sample Input Hello world I am a file Read me until end-of-file. Sample Output 1 Hello world 2 I am a file 3 Read me until end-of-file. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution {     public static void main(String[] args) {         /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */         Scanner scan = new Scanner(System.in);         int i=1;         while(scan.hasNext()){             System.out.println(i + " " + scan.nextLine());             i++;         }     } }

There are three lines of output: For the first line, sum the lengths of A and B. For the second line, write Yes if A is lexicographically greater than B otherwise print No instead. For the third line, capitalize the first letter in both A and B print them on a single line, separated by a space.

CSZONE import java.io.*; import java.util.*; public class Solution { public static String capitalize(String str){ if(str == null ){ return str; }else{ return str.substring(0,1).toUpperCase()+ str.substring(1).toLowerCase(); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); String A=sc.next(); String B=sc.next(); /* Enter your code here. Print output to STDOUT. */ System.out.println( A.length()+B.length()); if(A.compareTo(B)>0){ System.out.println("Yes"); }else{ System.out.println("No"); } System.out.println(capitalize(A)+" "+capitalize(B)); } }

In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:

CSZONE import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int N = scanner.nextInt(); if(N%2 !=0){ System.out.print("Weird"); }else if(N%2==0 && N >=2 && N<=5){ System.out.print("Not Weird"); }else if(N%2==0 && N >=6 && N<=20){ System.out.print("Weird"); }else if(N%2==0 && N>=20){ System.out.print("Not Weird"); } } }

Task Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

CSZONE import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the solve function below. static void solve(double meal_cost, int tip_percent, int tax_percent) { double total = meal_cost+meal_cost*((tip_percent/100.0)+(tax_percent/100.0)); System.out.print((int) Math.round(total)); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { double meal_cost = scanner.nextDouble(); int tip_percent = scanner.nextInt(); int tax_percent = scanner.nextInt(); solve(meal_cost, tip_percent, tax_percent); scanner.close(); } }