0 thoughts on “

  • Rajaul Islam
    March 16, 2020 at 7:30 pm
    Permalink

    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));
    }
    }
    }

  • Rajaul Islam
    March 16, 2020 at 7:20 pm
    Permalink

    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 &lt; ‘ ‘ || character &gt; ‘Z’ || encoding == null || encoding.length() == 0) {
    throw new Exception(String.format(&quot;The character %s is not a supported Morse character&quot;, 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&lt;MorseCode&gt; listCodes;
    private final ArrayList&lt;MorseCode&gt; listMorseCodes;

    public MorseCodeConvert(final String conversionFile) throws Exception {
    this.listCodes = new ArrayList&lt;MorseCode&gt;();
    this.listMorseCodes = new ArrayList&lt;MorseCode&gt;();
    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() &gt; 0) {
    final String[] arr = line.split(&quot;\t&quot;);
    if (arr.length == 2 &amp;&amp; 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(&quot;Invalid line: %s%n&quot;, 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(&quot;Failed to open file: %s&quot;, conversionFile));
    }
    }

    public void printEncodingList() {
    for (final MorseCode mc : this.listCodes) {
    System.out.printf(&quot;(‘%s’, %s)%n&quot;, mc.getCharacter(), mc.getEncoding());
    }
    }

    public void encodeString(final String line) {
    if (line != null &amp;&amp; line.length() &gt; 0) {
    char[] charArray;
    for (int length = (charArray = line.toUpperCase().toUpperCase().toCharArray()).length, i = 0; i &lt; length; ++i) {
    final char ch = charArray[i];
    if (ch != ‘ ‘) {
    final MorseCode morseCode = this.find(ch);
    if (morseCode == null) {
    System.out.print(&quot;?&quot;);
    } else {
    System.out.print(morseCode.getEncoding());
    }
    System.out.print(&quot; &quot;);
    }
    }
    }
    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(&quot;Failed to open file: %s&quot;, fileName));
    }
    }
    }

    [/code]

  • Rajaul Islam
    June 22, 2017 at 11:43 am
    Permalink

    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);

Leave a Reply

X