Slp28

 Q1) Write a program that reads on file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes. [10 marks] 

import java.io.File;

import java.util.Scanner;


public class Slip28_1

{

    public static void main(String[] args) 

    {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the file name (with full path): ");

        String fileName = scanner.nextLine();

        File file = new File(fileName);

        if (file.exists()) 

{

            System.out.println("File exists.");

            System.out.println("Readable: " + file.canRead());

            System.out.println("Writable: " + file.canWrite());

    System.out.println(file.isFile() ? "is normal file" : "might be a named pipe");

            System.out.println("It is " + (file.isDirectory() ? "" : "not" + " a directory"));

            System.out.println("File Length: " + file.length() + " bytes");      

        }

        scanner.close();

    }

}


Q2) Write a program called SwingTemperatureConverter to convert temperature values between Celsius and Fahrenheit. User can enter either the Celsius or the Fahrenheit value, in floating-point number. Hints: To display a floating-point number in a specific format (e.g., 1 decimal place), use the static method String.format(), which has the same form as printf(). For example, String.format("%.1f", 1.234) returns String "1.2". [20 marks]

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Slip28_2 extends JFrame {
    JTextField ctext;
    JTextField ftext;

    public Slip28_2() {
        // Set up the frame
        setTitle("Temperature Converter");
        setSize(300, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2, 2));

        // Create labels and text fields
        JLabel l1 = new JLabel("Celsius:");
        JLabel l2 = new JLabel("Fahrenheit:");

        ctext = new JTextField();
        ftext = new JTextField();

        // Add the components to the frame
        add(l1);
        add(ctext );
        add(l2);
        add(ftext);
setVisible(true);
        ftext.addActionListener(new ActionListener() {
           
            public void actionPerformed(ActionEvent e) {
                try {
                double fahrenheit = Double.parseDouble(ftext.getText());
                double celsius = (fahrenheit - 32) * 5 / 9;
                ctext.setText(String.format("%.1f", celsius));
            } catch (NumberFormatException ex) {       }     
            }
        });
ctext.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                try {
                double celsius = Double.parseDouble(ctext.getText());
                double fahrenheit = (celsius * 9 / 5) + 32;
                ftext.setText(String.format("%.1f", fahrenheit ));
            } catch (NumberFormatException ex) {       }     
            }
        });
    
    }

    public static void main(String[] args) {
        new Slip28_2();
    }
}






Comments

Popular posts from this blog

Slip1

Slip3

Slip2