Answer is:
var startingNumber;
var endingNumber;
var oddNumbers;
var sum=0;
function calculateSum(startingNumber, endingNumber){
//First Start loop for 1 to 10
for(startingNumber; startingNumber<=endingNumber; startingNumber++){
//Sort odd numbers from 1 to 10 numbers
if(startingNumber%2!=0){
//skip 3
if(startingNumber==3)
continue;
//skip 5
if(startingNumber==5)
continue;
//Sum of odd numbers except 3 and 5 from 1 to 10
document.writeln("Odd number: "+startingNumber+" “);
sum+=startingNumber;
}
}
document.writeln(“SUM of Odd Numbers: “+sum);
}
calculateSum(1,10);
thanks you sir,
Problem is solved!!
MorseCode.java
[code lang=”java”]
public class MorseCode
{
private char character;
private String encoding;
public MorseCode(final char character, final String encoding) throws Exception {
if (character < ‘ ‘ || character > ‘Z’ || encoding == null || encoding.length() == 0) {
throw new Exception(String.format("The character %s is not a supported Morse character", character));
}
this.character = character;
this.encoding = encoding;
}
public char getCharacter() {
return this.character;
}
public void setCharacter(final char character) {
this.character = character;
}
public String getEncoding() {
return this.encoding;
}
public void setEncoding(final String encoding) {
this.encoding = encoding;
}
}
[/code]
MorseCodeConvert.java
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class MorseCodeConvert {
private final ArrayList<MorseCode> listCodes;
private final ArrayList<MorseCode> listMorseCodes;
public MorseCodeConvert(final String conversionFile) throws Exception {
this.listCodes = new ArrayList<MorseCode>();
this.listMorseCodes = new ArrayList<MorseCode>();
try {
Throwable t = null;
try {
final Scanner scanner = new Scanner(new File(conversionFile));
try {
while (scanner.hasNext()) {
final String line = scanner.nextLine();
if (line.length() > 0) {
final String[] arr = line.split("\t");
if (arr.length == 2 && arr[0].length() == 1) {
try {
this.listCodes.add(new MorseCode(arr[0].charAt(0), arr[1]));
} catch (Exception ex) {
System.err.println(ex.toString());
}
} else {
System.err.printf("Invalid line: %s%n", line);
}
}
}
} finally {
if (scanner != null) {
scanner.close();
}
}
} finally {
if (t == null) {
final Throwable exception = null;
t = exception;
} else {
final Throwable exception = null;
if (t != exception) {
t.addSuppressed(exception);
}
}
}
} catch (FileNotFoundException e) {
throw new Exception(String.format("Failed to open file: %s", conversionFile));
}
}
public void printEncodingList() {
for (final MorseCode mc : this.listCodes) {
System.out.printf("(‘%s’, %s)%n", mc.getCharacter(), mc.getEncoding());
}
}
public void encodeString(final String line) {
if (line != null && line.length() > 0) {
char[] charArray;
for (int length = (charArray = line.toUpperCase().toUpperCase().toCharArray()).length, i = 0; i < length; ++i) {
final char ch = charArray[i];
if (ch != ‘ ‘) {
final MorseCode morseCode = this.find(ch);
if (morseCode == null) {
System.out.print("?");
} else {
System.out.print(morseCode.getEncoding());
}
System.out.print(" ");
}
}
}
System.out.println();
}
private MorseCode find(final char ch) {
for (final MorseCode mc : this.listCodes) {
if (mc.getCharacter() == ch) {
return mc;
}
}
return null;
}
public void encodeFile(final String fileName) throws Exception {
try {
Throwable t = null;
try {
final Scanner scanner = new Scanner(new File(fileName));
try {
while (scanner.hasNext()) {
this.encodeString(scanner.nextLine());
}
} finally {
if (scanner != null) {
scanner.close();
}
}
} finally {
if (t == null) {
final Throwable exception = null;
t = exception;
} else {
final Throwable exception = null;
if (t != exception) {
t.addSuppressed(exception);
}
}
}
} catch (FileNotFoundException e) {
throw new Exception(String.format("Failed to open file: %s", fileName));
}
}
}
MorseCode.java
[code lang=”java”]
public class MorseCode
{
private char character;
private String encoding;
public MorseCode(final char character, final String encoding) throws Exception {
if (character < ‘ ‘ || character > ‘Z’ || encoding == null || encoding.length() == 0) {
throw new Exception(String.format("The character %s is not a supported Morse character", character));
}
this.character = character;
this.encoding = encoding;
}
public char getCharacter() {
return this.character;
}
public void setCharacter(final char character) {
this.character = character;
}
public String getEncoding() {
return this.encoding;
}
public void setEncoding(final String encoding) {
this.encoding = encoding;
}
}
[/code]
MorseCodeConvert.java
[code lang=”java”]
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class MorseCodeConvert {
private final ArrayList<MorseCode> listCodes;
private final ArrayList<MorseCode> listMorseCodes;
public MorseCodeConvert(final String conversionFile) throws Exception {
this.listCodes = new ArrayList<MorseCode>();
this.listMorseCodes = new ArrayList<MorseCode>();
try {
Throwable t = null;
try {
final Scanner scanner = new Scanner(new File(conversionFile));
try {
while (scanner.hasNext()) {
final String line = scanner.nextLine();
if (line.length() > 0) {
final String[] arr = line.split("\t");
if (arr.length == 2 && arr[0].length() == 1) {
try {
this.listCodes.add(new MorseCode(arr[0].charAt(0), arr[1]));
} catch (Exception ex) {
System.err.println(ex.toString());
}
} else {
System.err.printf("Invalid line: %s%n", line);
}
}
}
} finally {
if (scanner != null) {
scanner.close();
}
}
} finally {
if (t == null) {
final Throwable exception = null;
t = exception;
} else {
final Throwable exception = null;
if (t != exception) {
t.addSuppressed(exception);
}
}
}
} catch (FileNotFoundException e) {
throw new Exception(String.format("Failed to open file: %s", conversionFile));
}
}
public void printEncodingList() {
for (final MorseCode mc : this.listCodes) {
System.out.printf("(‘%s’, %s)%n", mc.getCharacter(), mc.getEncoding());
}
}
public void encodeString(final String line) {
if (line != null && line.length() > 0) {
char[] charArray;
for (int length = (charArray = line.toUpperCase().toUpperCase().toCharArray()).length, i = 0; i < length; ++i) {
final char ch = charArray[i];
if (ch != ‘ ‘) {
final MorseCode morseCode = this.find(ch);
if (morseCode == null) {
System.out.print("?");
} else {
System.out.print(morseCode.getEncoding());
}
System.out.print(" ");
}
}
}
System.out.println();
}
private MorseCode find(final char ch) {
for (final MorseCode mc : this.listCodes) {
if (mc.getCharacter() == ch) {
return mc;
}
}
return null;
}
public void encodeFile(final String fileName) throws Exception {
try {
Throwable t = null;
try {
final Scanner scanner = new Scanner(new File(fileName));
try {
while (scanner.hasNext()) {
this.encodeString(scanner.nextLine());
}
} finally {
if (scanner != null) {
scanner.close();
}
}
} finally {
if (t == null) {
final Throwable exception = null;
t = exception;
} else {
final Throwable exception = null;
if (t != exception) {
t.addSuppressed(exception);
}
}
}
} catch (FileNotFoundException e) {
throw new Exception(String.format("Failed to open file: %s", fileName));
}
}
}
[/code]
Thank you sir for good article about JavaScript.
Answer is:
var startingNumber;
var endingNumber;
var oddNumbers;
var sum=0;
function calculateSum(startingNumber, endingNumber){
//First Start loop for 1 to 10
for(startingNumber; startingNumber<=endingNumber; startingNumber++){ //Sort odd numbers from 1 to 10 numbers if(startingNumber%2!=0){ //skip 3 if(startingNumber==3) continue; //skip 5 if(startingNumber==5) continue; //Sum of odd numbers except 3 and 5 from 1 to 10 document.writeln("Odd number: "+startingNumber+"
“);
sum+=startingNumber;
}
}
document.writeln(“SUM of Odd Numbers: “+sum);
}
calculateSum(1,10);
yes i shall explain write details soon.
can you explain a little more?
Simple!
DO you have plan to continue your writings on generics?
Thanks to share your knowledge. If you Explain a little, it will be helpful for us.