NPTEL Programming in Java Week8 Assignment Solution July 2023

NPTEL-Programming-in-Java-Week8-Assignment-Solution-July-2023

NPTEL Programming in Java Week8 All Programming Assignment Solutions – July 2023 | Swayam. With the growth of Information and Communication Technology, there is a need to develop large and complex software.

Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.

Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems.

This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.

COURSE LAYOUT
    • Week 1 : Overview of Object-Oriented Programming and Java
    • Week 2 : Java Programming Elements
    • Week 3 : Input-Output Handling in Java
    • Week 4 : Encapsulation
    • Week 5 : Inheritance
    • Week 6 : Exception Handling
    • Week 7 : Multithreaded Programming
    • Week 8 : Java Applets and Servlets
    • Week 9 : Java Swing and Abstract Windowing Toolkit (AWT)
    • Week 10 : Networking with Java
    • Week 11: Java Object Database Connectivity (ODBC)
    • Week 12: Interface and Packages for Software Development

Course Name : “Programming in Java 2023”

Question : 1 Write a program which will print a pyramid of “*” ‘s of height “n” and print the number of “*” ‘s in the pyramid.

For example:
 
Input : 5
Output :  
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
25
				
					import java.util.*;
public class Pattern1 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int n = inr.nextInt();
        int k = 0,sum=0;
        for(int i = 1; i <= n; ++i, k = 0) 
        {
            for(int space = 1; space <= n - i; ++space) 
            {
                System.out.print("  ");
            }
            while(k != 2 * i - 1) 
            {
                System.out.print("* ");
                sum=sum+1;
                ++k;
            }
            System.out.println();
        }
        System.out.print(sum); 
    }
}
				
			

Course Name : “Programming in Java 2023”

Question : 2 Write a program which will print a pascal  pyramid of  “*” ‘s of height “l” .

For example:

Input : 8
Output :
       * 
      * * 
     * * * 
    * * * * 
   * * * * * 
  * * * * * * 
 * * * * * * * 
* * * * * * * * 
				
					import java.util.*;
public class Pattern2 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int l = inr.nextInt();
        int i,j;
        int space=l-1;
        for(i=0;i< l;i++)
        {
            for(j=0;j< space;j++)
            {
                System.out.print(" ");
            }
            for(j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
            space--;
         }
     }
}
				
			

Course Name : “Programming in Java 2023”

Question : 3 Write a program which will print a pyramid of “numbers” ‘s of height “n” and print the sum of all number’s in the pyramid.

For example:
 
Input : 5
Output
        1 
      1 2 3 
    1 2 3 4 5 
  1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 9 
95
				
					import java.util.*;
public class Pattern3 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int n = inr.nextInt();
        int k = 1,sum=0;
        for(int i = 1; i <= n; ++i, k = 1) 
        {
            for(int space = 1; space <= n-i; ++space)
            {
                System.out.print("  ");
            }
            while(k <= 2 * i - 1) 
            {
                System.out.print(k+" ");
                sum=sum+k;
                ++k;
            }
            System.out.println();
        }
        System.out.print(sum); 
    }
}
				
			

Course Name : “Programming in Java 2023”

Question : 4 Write a program to print symmetric Pascal’s triangle of “*” ‘s of  height “l” of odd length . If input “l” is even then your program will print “Invalid line number”.

For example:

Input : 5
Output :
  *
 * *
* * *
 * *
  *
				
					import java.util.*;
public class Pattern4 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	  	int l = inr.nextInt();
        int ul=0; // Upper Line
        int ll=0; // Lower Line

        if (l%2!=0)
        {
           ul=(l/2)+1;			
           ll=l-ul;
           for(int i=1;i<=ul; i++)
           {
          		for(int s=1;s<=(ul-i); s++)
                {
              		System.out.print(" ");
          		}
          		for(int j=1;j<=i; j++)
                {
               		System.out.print("* ");
          		}
          		System.out.println();
          }

          int llc=ll;
          for(int i=1;i<=ll; i++)
          {
          		for(int s=llc;s<ll; s++)
                {
             		System.out.print(" ");
          		}
          		for(int j=1;j-1<=ll-i; j++)
                {
             		System.out.print(" *");
           		}
           		llc--;
          		System.out.println();
          }
       }
       else
       {
       		System.out.print("Invalid line number");
       }
    }
}
				
			

Course Name : “Programming in Java 2023”

Question : 5 Write a program to display any digit(n) from 0-9 represented as a “7 segment  display”

For example:
 
Input : 5
Output :
 _ 
|_ 
 _|
				
					import java.util.*;
public class Pattern5{
   private static final Map<Integer,Integer> encodings=new HashMap<Integer,Integer>();

    static{
        encodings.put(0, 0x7E);
        encodings.put(1, 0x30);
        encodings.put(2, 0x6D);
        encodings.put(3, 0x79);
        encodings.put(4, 0x33);
        encodings.put(5, 0x5B);
        encodings.put(6, 0x5F);
        encodings.put(7, 0x70);
        encodings.put(8, 0x7F);
        encodings.put(9, 0x7B);
    }

    public static void printDigit(int digit)
    {
        int code = encodeDigit(digit);
        char[] bits = String.format("%7s",Integer.toBinaryString(code))
          .replace(' ','0').toCharArray();

        lightSegmentDisp(bits[0] == '1', " _ \n", "   \n");
        lightSegmentDisp(bits[5] == '1', "|", " ");
        lightSegmentDisp(bits[6] == '1', "_", " ");
        lightSegmentDisp(bits[1] == '1', "|\n", " \n");
        lightSegmentDisp(bits[4] == '1', "|", " ");
        lightSegmentDisp(bits[3] == '1', "_", " ");
        lightSegmentDisp(bits[2] == '1', "|\n", " \n");
    }

    private static void lightSegmentDisp(boolean On, String onVal, String offVal)
    {
        System.out.print(On ? onVal : offVal);
        try {
            Thread.sleep(0);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static int encodeDigit(int d) {
        return encodings.containsKey(d) ? encodings.get(d) : 0x00;
    }
   public static void main(String[] args) throws Exception {
           Scanner inr = new Scanner(System.in);
	   	   int n = inr.nextInt();
           printDigit(n);
    }
}