Question

In: Computer Science

JAVA PROGRAMMING source code using appropriate programming style (e.g., descriptive variable names, indenting, comments, etc.). 1....

JAVA PROGRAMMING

source code using appropriate programming style (e.g., descriptive variable names, indenting, comments, etc.).

1. Using the remainder (%) operator, write a program that inputs an integer and displays if the number is even or odd.

B. Write the same program using some other technique without using the remainder (%) operator.

Solutions

Expert Solution

Java Program:

(1)

import java.util.*;

class EvenOrOdd_1
{
   public static void main(String[] args)
   {
       int n;
      
       //Scanner class object
       Scanner reader = new Scanner(System.in);
      
       //Reading n value from user
       System.out.print("Enter n: ");
       n = reader.nextInt();
      
       //Checking for even or odd using % operator
       if(n%2 == 0)
       {
           System.out.printf(" %d is even ", n);
       }
       else
       {
           System.out.printf(" %d is odd ", n);
       }
   }
}

Sample Run:

_______________________________________________________________________________________________________

(B)

import java.util.*;

class EvenOrOdd_2
{
   public static void main(String[] args)
   {
       int n;
      
       //Scanner class object
       Scanner reader = new Scanner(System.in);
      
       //Reading n value from user
       System.out.print("Enter n: ");
       n = reader.nextInt();
      
       //Checking for even or odd using / and * operator
       if( (n/2)*2 == n)
       {
           System.out.printf(" %d is even ", n);
       }
       else
       {
           System.out.printf(" %d is odd ", n);
       }
   }
}

Sample Run:


Related Solutions

Your Tasks Step 1 Write the comments in the SnapCracklePop.java using the java doc style in...
Your Tasks Step 1 Write the comments in the SnapCracklePop.java using the java doc style in zyBooks 3.8. See TODOa to TODOh.   Step 2 Complete the class definition, variable declarations, constructor, and all the methods by reading the instructions in SnapCracklePop.java. See TODO1 to TODO7. (DONE I NEED HELP WITH STEP 1) 1 //TODOa complete this javadoc comment 2 /** 3 * [The class description] 4 * @author [Your Name] 5 * @version 1.0 6 */ 7 8 //TODO1: declare...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
simple Java project// All variables have descriptive names or comments describing their purpose. Thank you Read...
simple Java project// All variables have descriptive names or comments describing their purpose. Thank you Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The...
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that...
Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that you have used/reused in any of your previous projects and how it contributes to the overall project.
This code works fine. In java, can you: change the variable and method names to make...
This code works fine. In java, can you: change the variable and method names to make logical sense add method header comments to describe what the method does and what the params/returns are add a class header comment to describe what the code does import java.util.Arrays; import java.util.Scanner; public class MethodLibrary { public static void main(String args[]) { System.out.println("MethodLibrary"); System.out.println("Example use of methods"); int[] smallNums1 = {2, 1, 7, 4, 3}; display("display:", smallNums1); char [] characters = {'a','b','c','d','e','f'}; first( characters,...
The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date. 1. (Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun. 2. (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT